全球快资讯:Python之⾯向对象-继承二

2022-12-20 15:02:42 来源:51CTO博客


(相关资料图)

六、多层继承

故事:N年后,daqiu⽼了,想要把所有技术传承给⾃⼰的徒弟。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 = "[独创煎饼果⼦配⽅]" 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): passxiaoqiu = Tusun()xiaoqiu.make_cake()xiaoqiu.make_school_cake()xiaoqiu.make_master_cake()

七、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(School, self).__init__()   # super(School, self).make_cake()   # ⽅法2.2   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)    # ⼀次性调⽤⽗类的同名属性和⽅法 def make_old_cake(self):   # ⽅法⼀:代码冗余;⽗类类名如果变化,这⾥代码需要频繁修改   # Master.__init__(self)   # Master.make_cake(self)   # School.__init__(self)   # School.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()注意:使⽤super() 可以⾃动查找⽗类。调⽤顺序遵循 __mro__ 类属性的顺序。⽐较适合单继承使⽤。

八、私有权限

8.1 定义私有属性和⽅法

在Python中,可以为实例属性和⽅法设置私有权限,即设置某个实例属性或实例⽅法不继承给⼦类。故事:daqiu把技术传承给徒弟的同时,不想把⾃⼰的钱(2000000个亿)继承给徒弟,这个时候就要为 钱 这个实例属性设置私有权限。设置私有权限的⽅法:在属性名和⽅法名 前⾯ 加上两个下划线 __。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 # 定义私有⽅法 def __info_print(self):   print(self.kongfu)   print(self.__money) 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 daqiu = Prentice()# 对象不能访问私有属性和私有⽅法# print(daqiu.__money)# daqiu.__info_print()xiaoqiu = Tusun()# ⼦类⽆法继承⽗类的私有属性和私有⽅法# print(xiaoqiu.__money) # ⽆法访问实例属性__money# xiaoqiu.__info_print()注意:私有属性和私有⽅法只能在类⾥⾯访问和修改

8.2 获取和修改私有属性值

在Python中,⼀般定义函数名 get_xx ⽤来获取私有属性,定义 set_xx ⽤来修改私有属性值。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   # 获取私有属性 def get_money(self):   return self.__money # 修改私有属性 def set_money(self):   self.__money = 500 def __info_print(self):   print(self.kongfu)   print(self.__money) 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): passdaqiu = Prentice()xiaoqiu = Tusun()# 调⽤get_money函数获取私有属性money的值print(xiaoqiu.get_money())# 调⽤set_money函数修改私有属性money的值xiaoqiu.set_money()print(xiaoqiu.get_money())

标签: 属性设置 这个时候

上一篇:Python之⾯向对象其它
下一篇:环球快讯:如何使用 Spring Boot 构建一个简单的 Web 应用程序