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
028.Python面向对象继承(单继承,多继承,super,菱形继承)的更多相关文章
- C++//菱形继承 //俩个派生类继承同一个基类 //又有某个类同时继承俩个派生类 //成为 菱形继承 或者 钻石 继承//+解决
1 //菱形继承 2 //俩个派生类继承同一个基类 3 //又有某个类同时继承俩个派生类 4 //成为 菱形继承 或者 钻石 继承 5 6 #include <iostream> 7 #i ...
- Python面向对象(组合、菱形继承、多态)
今日内容: 1.组合 2.菱形继承 3.多态与多态性 昨天内容重点回顾: 1)调用也叫实例化:发生了2件事 1.创造空对象 2.触发对象下的__init__方法,然后将p连同参数一同传给init ...
- C++反汇编第五讲,认识多重继承,菱形继承的内存结构,以及反汇编中的表现形式.
C++反汇编第五讲,认识多重继承,菱形继承的内存结构,以及反汇编中的表现形式. 目录: 1.多重继承在内存中的表现形式 多重继承在汇编中的表现形式 2.菱形继承 普通的菱形继承 虚继承 汇编中的表现形 ...
- C++反汇编-菱形继承
学无止尽,积土成山,积水成渊-<C++反汇编与逆向分析技术揭秘> 读书笔记.马上就要出差了,回来后接着写吧. 一.概述 菱形继承是最复杂的对象结构,菱形结构会将单一继承与多重继承进行组合. ...
- day35-2 类的三大特性---多态,以及菱形继承问题
目录 菱形继承问题 经典类 新式类 菱形继承 大招 多态与多态性 多态 多态性 多态在Python中的体现 鸭子类型(重要) 结论 菱形继承问题 经典类 没有继承object类的就是经典类,只有Pyt ...
- C++反汇编第四讲,认识多重继承,菱形继承的内存结构,以及反汇编中的表现形式.
目录: 1.多重继承在内存中的表现形式 多重继承在汇编中的表现形式 2.菱形继承 普通的菱形继承 虚继承 汇编中的表现形式 一丶多重继承在内存中的表现形式 高级代码: class Father1 { ...
- Python面向对象中super用法与MRO机制
1. 引言 最近在研究django rest_framework的源码,老是遇到super,搞得一团蒙,多番查看各路大神博客,总算明白了一点,今天做一点总结. 2. 为什么要用super 1)让代码维 ...
- Python进阶(十六)----面向对象之~封装,多态,鸭子模型,super原理(单继承原理,多继承原理)
Python进阶(十六)----面向对象之~封装,多态,鸭子模型,super原理(单继承原理,多继承原理) 一丶封装 , 多态 封装: 将一些东西封装到一个地方,你还可以取出来( ...
- Python进阶(十五)----面向对象之~继承(单继承,多继承MRO算法)
Python进阶(十五)----面向对象之~继承 一丶面向对象的三大特性:封装,继承,多态 二丶什么是继承 # 什么是继承 # b 继承 a ,b是a的子类 派生类 , a是b的超类 基类 父类 # ...
随机推荐
- 史上最全jdk新特性总结,涵盖jdk8到jdk15!
前言 在本文中,我将描述自第8版以来Java最重要且对开发人员友好的功能.为什么会有这样的主意?在Web上,您可以找到许多文章,其中包含每种Java版本的新功能列表.但是,由于缺少文章,因此无法简要概 ...
- ES9的新特性:异步遍历Async iteration
ES9的新特性:异步遍历Async iteration 目录 简介 异步遍历 异步iterable的遍历 异步iterable的生成 异步方法和异步生成器 简介 在ES6中,引入了同步iteratio ...
- 敏捷史话(十四):敏捷之峰的攀登者 —— Jim Highsmith
"我们希望,一起组成的敏捷联盟能够帮助到其他同行,帮他们用新的更'敏捷'的方式去思考软件开发.方法论和组织.做到这一点,我们就得偿所愿了."Jim Highsmith 在雪鸟会议结 ...
- @babel/preset-env使用polyfill遇到的坑
场景还原 最近将一个项目由babel@6升级到babel@7,升级后最重要的两个包: @babel/preset-env: 提供代码的转换和API的polyfill的能力 @babel/plugin- ...
- get_started_3dsctf_2016-Pwn
get_started_3dsctf_2016-Pwn 这个题确实有点坑,在本地能打,在远程就不能打了,于是我就换了另一种方法来做. 确这个题是没有动态链接库,且PIE是关的,所以程序的大部分地址已经 ...
- unzip解压中文乱码
1 问题描述 直接 unzip xxx.zip 乱码,肯定是编码问题了不用问.但是unzip没有指定编码的选项: 网上的解决方案如下: unzip -O GBK/GB18030CP936 xx.zip ...
- k8s helm 安装etcd
待续 helm install etcd bitnami/etcd \ --set statefulset.replicaCount=3 \ --set persistence.enabled=tru ...
- 1055 The World's Richest
Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...
- 14- 小程序测试与airtest自动化测试
什么是小程序 小程序是一种不需要下载安装即可使用的应用,它实现了应用"触手可及"的梦想,用户扫一扫或者搜一下即可打开应用.也体现了"用完即走"的理念,用户不用关 ...
- 关于CSS3背景渐变色无效问题
无效的css[linear-gradient]写法 .loginbox{ background-color: linear-gradient(#D0D0D0, #E0E0E0, white); wid ...