08
08
08|初识面向对象
zip()
names = ['小赵', '小钱', '小李']
ages = [18, 16, 20]
for name, age in zip(names, ages):
print(name, age)
Python_2.0
类
class ClassName_1:
pass
class ClassName_2(object):
pass
动态设置实例属性
class Author:
pass
author = Author()
author.name = 'follow__my_heart'
author.age = 20
print(author.name, author.age)
实例方法
self
Python
中的self
在编程规则下代表的是实例对象本身,本质其实只是一个普通的参数
class Author:
def __init__(self, name, age):
print(self)
self.name = name
self.age = age
def print_self(self):
print(self)
author = Author('follow__my_heart', 20)
author.print_self()
Author.print_self('嘤嘤嘤...')
09
09
09|深入面向对象
类访问实例方法
- 类本身可以访问所有方法,但在访问实例方法的时候需要把实例对象作为一个参数进行传递
类对象和实例对象存储的数据
类对象
实例对象
- 实例属性
- 类对象地址:
__class__
属性存储在实例对象中,用来保存类对象地址
类的私有属性
class Author:
def __init__(self, name, age):
self.name = name
self.__age = age
def info(self):
print(f'name: {self.name}, age: {self.__age}')
def is_age(self):
if self.__age < 0:
self.__age = None
return False
return True
author = Author('follow__my_heart', -1)
if author.is_age():
print('年龄数据有效')
else:
print('年龄数据无效')
author.info()
类的私有方法
class Author:
def __init__(self, name, age, money):
self.name = name
self.age = age
self.__money = money
def info(self):
print(f'name: {self.name}, age: {self.age}')
def __get_money_func(self):
print(f'有 {self.__money}¥')
def get_money(self):
self.__get_money_func()
author = Author('follow__my_heart', 20, 1000000)
author.get_money()
继承
- 子类不继承父类的属性,运行了父类的初始化方法后才拥有父类的属性
多继承
class Camera:
def take_photo(self):
print('拍照...')
class Movie:
def watch_movie(self):
print('看电影...')
class PhoneOS(Camera, Movie):
def call(self):
print('打电话...')
phone = PhoneOS()
phone.call()
phone.watch_movie()
phone.take_photo()
10
10
10|进阶面向对象
super()
class Father:
def __init__(self, name, age):
self.name = name
self.age = age
def play_game(self):
print('正在玩游戏...')
class Son(Father):
def __init__(self, name, age, gender):
super().__init__(name, age)
self.gender = gender
def play_game(self):
super().play_game()
print('正在玩王者荣耀...')
son = Son('follow__my_heart', 20, '男')
son.play_game()
魔术方法
__str__()
class Author:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f'姓名: {self.name}, 年龄: {self.age}'
author = Author('follow__my_heart', 20)
print(author)
__repr__()
In [1]: class Test:
···: def __repr__(self):
···: return '1'
···:
···:
···: test = Test()
In [2]: test
多态
静态方法
class Test:
@staticmethod
def info():
print('这是一个静态方法...')
test = Test()
test.info()
Test.info()
类属性
class Student:
stu_num = 0
def __init__(self, name):
self.name = name
Student.stu_num += 1
def info(self):
print(f'学生姓名: {self.name}, 学生总人数: {self.stu_num}')
stu = Student('follow__my_heart')
stu.info()
类方法
class Student:
stu_num = 0
def __init__(self, name):
self.name = name
Student.stu_num += 1
def info(self):
print(f'学生姓名: {self.name}')
@classmethod
def get_student_num(cls):
print(f'学生总人数: {cls.stu_num}')
@staticmethod
def get_cls_attr():
print(f'学生总人数: {Student.stu_num}')
stu = Student('follow__my_heart')
stu.get_student_num()
stu.get_cls_attr()
Student.get_student_num()
查看对象的所有属性和方法
class Student:
stu_num = 0
@classmethod
def cls_attr(cls):
return f'这是类方法返回的类属性的值: {cls.stu_num}'
stu = Student()
print(dir(stu))
钻石继承
普通方式
class Parent:
def __init__(self, name):
print('Parent 类的 init 开始被调用...')
self.name = name
print('Parent 类的 init 结束调用...')
class Son_1(Parent):
def __init__(self, name, age):
print('Son_1 类的 init 开始被调用...')
Parent.__init__(self, name)
self.age = age
print('Son_1 类的 init 结束调用...')
class Son_2(Parent):
def __init__(self, name, gender):
print('Son_2 类的 init 开始被调用...')
Parent.__init__(self, name)
self.gender = gender
print('Son_2 类的 init 结束调用...')
class GrandSon(Son_1, Son_2):
def __init__(self, name, age, gender):
print('GrandSon 类的 init 开始被调用...')
Son_1.__init__(self, name, age)
Son_2.__init__(self, name, gender)
print('GrandSon 类的 init 结束调用...')
grandson = GrandSon('follow__my_heart', 20, '男')
super()
方式
class Parent:
def __init__(self, name, *args, **kwargs):
print('Parent 类的 init 开始被调用...')
self.name = name
print('Parent 类的 init 结束调用...')
class Son_1(Parent):
def __init__(self, name, age, *args, **kwargs):
print('Son_1 类的 init 开始被调用...')
super().__init__(name, *args, **kwargs)
self.age = age
print('Son_1 类的 init 结束调用...')
class Son_2(Parent):
def __init__(self, name, gender, *args, **kwargs):
print('Son_2 类的 init 开始被调用...')
super().__init__(name, *args, **kwargs)
self.gender = gender
print('Son_2 类的 init 结束调用...')
class GrandSon(Son_1, Son_2):
def __init__(self, name, age, gender):
print('GrandSon 类的 init 开始被调用...')
super().__init__(name, age, gender)
print('GrandSon 类的 init 结束调用...')
grandson = GrandSon('follow__my_heart', 20, '男')
print(GrandSon.__mro__)
__mro__
保存的是类的继承顺序,super()
根据__mro__
进行调用
object
基类
- 在
Python 3
中所有的类都继承object
基类