# 如果是先调用了父类的属性和方法,父类属性会覆盖子类属性,故在调用属性前,先调用自己子类的初始化 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) daqiu = Prentice() daqiu.make_cake() daqiu.make_master_cake() daqiu.make_school_cake() daqiu.make_cake()
多层继承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): pass xiaoqiu = Tusun() xiaoqiu.make_cake() xiaoqiu.make_school_cake() xiaoqiu.make_master_cake()super(): 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 Premtice(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__类属性的顺序. 比较适合单继承使用私有属性和私有方法定义私有属性和方法: 在Python中 , 可以为实例属性和方法设置私有权限 , 即设置某个实例属性或实例方法不继承给子类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
推荐阅读
-
00后|环境优美、工作清闲,00后女生晒守墓工作惹人羡慕,评论区亮了
-
iPhone|设计师制作iPhone 14概念图:加入背屏感受下
-
【数据宝平台】最高看涨近翻倍(附股),高增长的超跌股名单!股价狂打折
-
-
品快点娱乐|当年他动不动就掀桌子,如今也是白发苍苍,怎么感觉有点像张纪中
-
喵喵次元仅有一型潜艇没有龟背,为何美国战略核潜艇没有龟背?别被骗了
-
教育未来|清华贫困生的“树洞”刷屏 该学生更新后续:网友看完直呼深受震撼
-
NVIDIA|NVIDIA发布RTX A2000专业卡:安培家族最迷你显卡降临
-
在非北大与清华的其他国内top10院校当学渣是一种怎么样的体验(比如人大,中大,南大等等)
-
护肤品|关之琳真是中年妇女穿搭的楷模,绿色外套配小红靴,洋气又有气质
-
『健美健身联盟』找个肌肉男朋友后,有多爽?,肌肉男女实力强啪
-
虎扑|之前三次德国球队全胜,欧战决赛第四次迎来德法之争
-
找靓机|首发LTPO显示技术!三星Note20马上就来:但变化也太大了吧?
-
-
爆笑小姐|感觉看到了嫦娥下凡一样,十条段子:看到个50岁妇女
-
爆笑部落|听着水声睡觉,搞笑GIF趣图:每天住在水帘洞里面
-
新宁德|宁德“弱鸟先飞、滴水穿石”30年突出贡献人物
-
娱乐中的趣闻|88碎片别乱花,三款高性价比史诗皮肤有望加入,周四碎片商店翻新
-
环球网娱乐频道|姚晨戴棒球帽配大框眼镜 穿黑色紧身裙青春减龄
-
井柏然|井柏然: 出生28天被弃, 60岁奶奶捡废品将他养大, 爆红后父母来认亲