1.类名.__mro__ 可打印参看函数的所有父类
2.super()方法
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果子配方]'
def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')
class School(Master):
def __init__(self):
self.kongfu = '[黑马煎饼果子配方]'
def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')
# 2.1 super()带参数写法
# super(School, self).__init__()
# super(School, self).make_cake()
# 2.2 无参数的super
super().__init__()
super().make_cake()
class Prentice(School):
def __init__(self):
self.kongfu = '[独创煎饼果子技术]'
def make_cake(self):
self.__init__()
print(f'运用{self.kongfu}制作煎饼果子')
# 子类调用父类的同名方法和属性:把父类的同名属性和方法再次封装
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake(self)
# 需求:一次性调用父类School Master的方法
def make_old_cake(self):
# 方法一:如果定义的类名修改,这里也要修改,麻烦; 代码量庞大,冗余
# School.__init__(self)
# School.make_cake(self)
# Master.__init__(self)
# Master.make_cake(self)
# 方法二:super()
# 2.1 super(当前类名, self).函数()
# super(Prentice, self).__init__()
# super(Prentice, self).make_cake()
# 2.2 无参数super
super().__init__()
super().make_cake()
daqiu = Prentice()
daqiu.make_old_cake()
3.设置私有属性:在属性名和方法名前面加上两个下划线__
获取和修改私有属性
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果子配方]'
def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')
class School(object):
def __init__(self):
self.kongfu = '[黑马煎饼果子配方]'
def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')
class Prentice(School, Master):
def __init__(self):
self.kongfu = '[独创煎饼果子技术]'
# 定义私有属性
self.__money = 2000000
# 定义函数:获取私有属性值 get_xx
def get_money(self):
return self.__money
# 定义函数:修改私有属性值 set_xx
def set_money(self):
self.__money = 500
# 定义私有方法
def __info_print(self):
print('这是私有方法')
def make_cake(self):
self.__init__()
print(f'运用{self.kongfu}制作煎饼果子')
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake(self)
class Tusun(Prentice):
pass
xiaoqiu = Tusun()
print(xiaoqiu.get_money())
xiaoqiu.set_money()
print(xiaoqiu.get_money())
4.类属性
优点 记录的某项数据 始终保持一致时,则定义类属性。 实例例属性要求每个对象为其单独开辟一份内存空间 来记录数据,而类属性为全类所共有,仅占用一份内存,更加节省内存空间。
类属性的修改
class Dog(object):
tooth = 10
wangcai = Dog()
xiaohei = Dog()
# 修改类属性
Dog.tooth = 12
print(Dog.tooth) # 12
print(wangcai.tooth) # 12
print(xiaohei.tooth) # 12
# 不能通过对象修改属性,如果这样操作,实则是创建了一个实例例属性
wangcai.tooth = 20
print(Dog.tooth) # 12
print(wangcai.tooth) # 20
print(xiaohei.tooth) # 12
实例属性
class Dog(object):
def __init__(self):
self.age = 5
def info_print(self):
print(self.age)
wangcai = Dog()
print(wangcai.age) # 5
# print(Dog.age) # 报错:实例例属性不不能通过类访问
wangcai.info_print() # 5
5.类方法@classmethod
当方法中 需要使用类对象 (如访问私有类属性等)时,定义类方法 类方法一般和类属性配合使用,当类属性为私有时,通过类方法可以获取,修改;
# 1. 定义类:私有类属性,类方法获取这个私有类属性
class Dog(object):
__tooth = 10
# 定义类方法
@classmethod
def get_tooth(cls):
return cls.__tooth
@classmethod
def set_toovh(cls):
cls.__tooth = 1000
# 2. 创建对象,调用类方法
wangcai = Dog()
result = wangcai.get_tooth()
print(result)
wangcai.set_toovh()
result = wangcai.get_tooth()
print(result)
6.静态方法@staticmethod 可以通过实例对象和类对象去访问
442

被折叠的 条评论
为什么被折叠?



