028.Python面向对象继承(单继承,多继承,super,菱形继承)
一 继承的概念
种类
- 单继承
- 多继承
至少两个类:
- 子类:一个类继承另外一个类,那么该类是子类(也叫作衍生类)
- 父类:另外一个,这个被继承的类,叫做父类(也叫作超类),object 在python中 这个类是所有类的父类
二 单继承
2.1 子父继承之后,子类可以使用父类的公有成员属性方法
class Plane(): capitain = "一般是男性" price = "一般保密" def fly(self): print("飞机都会飞") def capitain(self): print("一般都会有一个副驾驶员") def __price(self): print ("保密") # 想让一个类继承另外一个类,语法在定义类的时候,括号里面写(父类) class Fighter(Plane): pass obj = Fighter() print (obj.price) class Airliner(Plane): def Airliner_price(): self.__price
执行
[root@node10 python]# python3 test.py 一般保密
2.2 子父继承之后,子类不能调用父类的私有成员属性方法
class Plane(): capitain = "一般是男性" price = "一般保密" def fly(self): print("飞机都会飞") def capitain(self): print("一般都会有一个副驾驶员") def __price(self): print ("保密") class Fighter(Plane): pass obj = Fighter() print (obj.price) class Airliner(Plane): def Airliner_price(): self.__price obj = Airliner() obj.Airliner_price()
执行
调用私有方法
class Plane(): capitain = "一般是男性" price = "一般保密" def fly(self): print("飞机都会飞") def capitain(self): print("一般都会有一个副驾驶员") def __price(self): print ("保密") class Fighter(Plane): pass obj = Fighter() print (obj.price) class Airliner(Plane): def Airliner_price(): self.__price obj = Airliner() obj.__price
执行
[root@node10 python]# python3 test.py 一般保密 Traceback (most recent call last): File "test.py", line 22, in <module> obj.__price AttributeError: 'Airliner' object has no attribute '__price'
2.3 子父继承之后,子类可以改写父类的公有方法
- 如果子类当中含有该方法,那么优先调用子类的方法
- 如果子类当中不含有该方法,再去调用父类的方法.
有就调用自己的,没有就调用父类的(一定是共有的成员属性方法;)
class Plane(): capitain = "一般是男性" price = "一般保密" def fly(self): print("飞机都会飞") def capitain(self): print("一般都会有一个副驾驶员") def __price(self): print ("保密") class Fighter(Plane): def capitain(self): print ("机长是John") obj = Fighter() obj.capitain()
执行
[root@node10 python]# python3 test.py 机长是John
三 多继承
3.1 基本结构
class Picture(): p_style = "classic" def colour(self): print ("bule,green,gray,yellow") class Music(): m_style = "confitable" def Tone(self): print ("宫商角微羽") #继承两个父类 class Surroundings(Picture,Music): pass obj = Surroundings() print (obj.p_style) obj.Tone()
执行
[root@node10 python]# python3 test.py classic 宫商角微羽
3.2 super 调用父类的相关公有属性方法
- super本身是一个类 super()是一个对象 用于调用父类的绑定方法
- super() 只应用在绑定方法中,默认自动传递self对象 (前提:super所在作用域存在self)
- super用途: 解决复杂的多继承调用顺序
class Picture(): p_style = "classic" def colour(self): print ("bule,green,gray,yellow") class Music(): m_style = "confitable" def Tone(self): print ("宫商角微羽") class Surroundings(Picture,Music): p_style = "Modern" def s_tone(self): print(self.p_style) self.Tone() print (Picture.p_style) Music.Tone("123") def s_tone2(self): res = super().p_style print (res) super().Tone() obj = Surroundings() obj.s_tone()
执行
[root@node10 python]# python3 test.py classic 宫商角微羽 Modern 宫商角微羽
使用super
class Picture(): p_style = "classic" def colour(self): print ("bule,green,gray,yellow") class Music(): m_style = "confitable" def Tone(self): print ("宫商角微羽") class Surroundings(Picture,Music): p_style = "Modern" def s_tone(self): print(self.p_style) self.Tone() print (Picture.p_style) Music.Tone("123") def s_tone2(self): res = super().p_style print (res) super().Tone() obj = Surroundings() obj.s_tone2()
执行
[root@node10 python]# python3 test.py classic 宫商角微羽 classic 宫商角微羽
四 菱形继承
4.1 基本实例
class Human(): pty = 111 def feelT(self): print("1111") print(self.pty) print("2222") class Man(Human): pty = 222 def feelT(self): print("3333") super().feelT() print("4444") class Woman(Human): pty = 333 def feelT(self): print("5555") super().feelT() print("6666") class Children(Man,Woman): pty = 444 def feelT(self): print("7777") super().feelT() print("8888") obj = Children() obj.feelT() ''' # mro列表 类.mro() 使用c3算法,针对于多继承的情况,按照这个列表依次调用.调用顺序都在其中 # 如果出现重名方法, super() 就是按照这个列表依次调用 ''' res = Children.mro() print(res)
执行
7777 3333 5555 1111 444 2222 6666 4444 8888 [
<class '__main__.Children'>,
<class '__main__.Man'>,
<class '__main__.Woman'>,
<class '__main__.Human'>,
<class 'object'>
]
执行过程
- 调用obj.feelT(),这个是children的这个类,打印出7777
- 执行children类中方法的super().feelT(),但是每一个类都有FeelT的方法,使用Mro表(Children,man,woman,human),依次执行3333,5555,1111,但是停留在human的这个类上
- 执行print(self.pty),最初的是在children,打印出444
- 执行print("2222"),打印2222,这个类执行完,回到上一层的类woman
- 执行print("6666"),打印6666,则woman这个泪执行玩,回到上一层man
- 执行print("4444"),打印4444,则man的这个类执行完,回到上一层children
- 执行print("8888"),打印8888,整个过程执行,最后打印mro表
4.2 判断子父关系
class Human(): pty = 111 def feelT(self): print("1111") print(self.pty) print("2222") class Man(Human): pty = 222 def feelT(self): print("3333") super().feelT() print("4444") class Woman(Human): pty = 333 def feelT(self): print("5555") super().feelT() print("6666") class Children(Man,Woman): pty = 444 def feelT(self): print("7777") super().feelT() print("8888") obj = Children() obj.feelT() ''' # mro列表 类.mro() 使用c3算法,针对于多继承的情况,按照这个列表依次调用.调用顺序都在其中 # 如果出现重名方法, super() 就是按照这个列表依次调用 ''' res = Children.mro() print(res) res= issubclass(Children,Man) # 判断Children 是不是元组当中一个类的子类,有一个成立,返回真,一个都不满足,返回假 res = issubclass(Children,(Man,Woman)) print(res) # 只要有血缘关系即可. res = issubclass(Children,Human) print(res) # 判断obj这个对象类型是不是Children (有继承的血缘关系即可) ''' python当中,万物皆是对象,只是对象常常这两字被省略. ''' res = isinstance(obj,Children) print(res) res = isinstance(obj,Human) print(res) res = isinstance(obj,(Man,Woman)) print(res)
执行
7777 3333 5555 1111 444 2222 6666 4444 8888 [<class '__main__.Children'>, <class '__main__.Man'>, <class '__main__.Woman'>, <class '__main__.Human'>, <class 'object'>] True True True True True
作者:梦中泪
关于作者:云计算,linux,虚拟化,存储
---------------------------------------------------------------------------
个性签名:我以为我很颓废,今天我才知道,原来我早报废了。
如果觉得本篇文章最您有帮助,欢迎转载,且在文章页面明显位置给出原文链接!记得在右下角点个“推荐”,博主在此感谢!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· dotnet 9 通过 AppHostRelativeDotNet 指定自定义的运行时路径
· 如何统计不同电话号码的个数?—位图法
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 从零实现富文本编辑器#3-基于Delta的线性数据结构模型
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· 用c#从头写一个AI agent,实现企业内部自然语言数据统计分析
· 三维装箱问题(3D Bin Packing Problem, 3D-BPP)
· Windows上,10分钟构建一个本地知识库
· 使用 AOT 编译保护 .NET 核心逻辑,同时支持第三方扩展
· Java虚拟机代码是如何一步一步变复杂且难以理解的?