一 继承的概念

种类

  • 单继承
  • 多继承

至少两个类:

  • 子类:一个类继承另外一个类,那么该类是子类(也叫作衍生类)
  • 父类:另外一个,这个被继承的类,叫做父类(也叫作超类),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 调用父类的相关公有属性方法

  1. super本身是一个类 super()是一个对象 用于调用父类的绑定方法
  2. super() 只应用在绑定方法中,默认自动传递self对象 (前提:super所在作用域存在self)
  3. 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'>
]

执行过程

  1. 调用obj.feelT(),这个是children的这个类,打印出7777
  2. 执行children类中方法的super().feelT(),但是每一个类都有FeelT的方法,使用Mro表(Children,man,woman,human),依次执行3333,5555,1111,但是停留在human的这个类上
  3. 执行print(self.pty),最初的是在children,打印出444
  4. 执行print("2222"),打印2222,这个类执行完,回到上一层的类woman
  5. 执行print("6666"),打印6666,则woman这个泪执行玩,回到上一层man
  6. 执行print("4444"),打印4444,则man的这个类执行完,回到上一层children
  7. 执行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,菱形继承)的更多相关文章

  1. C++//菱形继承 //俩个派生类继承同一个基类 //又有某个类同时继承俩个派生类 //成为 菱形继承 或者 钻石 继承//+解决

    1 //菱形继承 2 //俩个派生类继承同一个基类 3 //又有某个类同时继承俩个派生类 4 //成为 菱形继承 或者 钻石 继承 5 6 #include <iostream> 7 #i ...

  2. Python面向对象(组合、菱形继承、多态)

    今日内容: 1.组合 2.菱形继承 3.多态与多态性 昨天内容重点回顾: 1)调用也叫实例化:发生了2件事  1.创造空对象  2.触发对象下的__init__方法,然后将p连同参数一同传给init  ...

  3. C++反汇编第五讲,认识多重继承,菱形继承的内存结构,以及反汇编中的表现形式.

    C++反汇编第五讲,认识多重继承,菱形继承的内存结构,以及反汇编中的表现形式. 目录: 1.多重继承在内存中的表现形式 多重继承在汇编中的表现形式 2.菱形继承 普通的菱形继承 虚继承 汇编中的表现形 ...

  4. C++反汇编-菱形继承

    学无止尽,积土成山,积水成渊-<C++反汇编与逆向分析技术揭秘> 读书笔记.马上就要出差了,回来后接着写吧. 一.概述 菱形继承是最复杂的对象结构,菱形结构会将单一继承与多重继承进行组合. ...

  5. day35-2 类的三大特性---多态,以及菱形继承问题

    目录 菱形继承问题 经典类 新式类 菱形继承 大招 多态与多态性 多态 多态性 多态在Python中的体现 鸭子类型(重要) 结论 菱形继承问题 经典类 没有继承object类的就是经典类,只有Pyt ...

  6. C++反汇编第四讲,认识多重继承,菱形继承的内存结构,以及反汇编中的表现形式.

    目录: 1.多重继承在内存中的表现形式 多重继承在汇编中的表现形式 2.菱形继承 普通的菱形继承 虚继承 汇编中的表现形式 一丶多重继承在内存中的表现形式 高级代码: class Father1 { ...

  7. Python面向对象中super用法与MRO机制

    1. 引言 最近在研究django rest_framework的源码,老是遇到super,搞得一团蒙,多番查看各路大神博客,总算明白了一点,今天做一点总结. 2. 为什么要用super 1)让代码维 ...

  8. Python进阶(十六)----面向对象之~封装,多态,鸭子模型,super原理(单继承原理,多继承原理)

    Python进阶(十六)----面向对象之~封装,多态,鸭子模型,super原理(单继承原理,多继承原理) 一丶封装 , 多态 封装:            将一些东西封装到一个地方,你还可以取出来( ...

  9. Python进阶(十五)----面向对象之~继承(单继承,多继承MRO算法)

    Python进阶(十五)----面向对象之~继承 一丶面向对象的三大特性:封装,继承,多态 二丶什么是继承 # 什么是继承 # b 继承 a ,b是a的子类 派生类 , a是b的超类 基类 父类 # ...

随机推荐

  1. JVMGC+Spring Boot生产部署和调参优化

    一.微服务开发完成,IDEA进行maven clean和package 出现BUILD SUCCESS说明打包成功 二.要求微服务启动时,配置JVM GC调优参数 p.p1 { margin: 0; ...

  2. OO_Unit2_Summary

    经过三周的自己电梯瞎设计,下次坐电梯想我想的可能就不是如何优化调度算法,而是千万别把自己死锁在电梯里了(手动狗头) 一.设计策略 1. 需求分析: 作业一:单部多线程可稍带电梯,一部电梯,固定楼层,不 ...

  3. (九)Struts2模型驱动和属性驱动

    出于结构清晰的考虑,应该采用单独的Model实例来封装请求参数和处理结果,这就是所谓的模型驱动, 所谓模型驱动,就是使用单独的JavaBean来贯穿整个MVC流程. 所谓属性驱动,就是使用属性来作为贯 ...

  4. SpringBoot项目war包部署

    服务部署 记录原因 将本地SpringBoot项目通过war包部署到虚拟机中,验证服务器部署. 使用war包是为了方便替换配置文件等. 工具 对象 版本 Spring Boot 2.4.0 VMwar ...

  5. Install Tensorflow object detection API in Anaconda (Windows)

    This blog is to explain how to install Tensorflow object detection API in Anaconda in Windows 10 as ...

  6. Java网络编程快速上手(SE基础)

    参考资料:百度百科TCP协议 本文涉及Java IO流.异常的知识,可参考我的另外的博客 一文简述Java IO 一文简述JAVA内部类和异常 1.概述 计算机网络相关知识: OSI七层模型 一个报文 ...

  7. Windows命令行学习(系统信息收集)

    echo off :关闭回显 echo on :开启回显 net user :显示主机的电脑用户 ipconfig /displaydns:后面的 /displaydns是显示当前电脑所缓存的dns信 ...

  8. 19. slot插槽传递模板

    插槽,也就是slot,是组件的一块HTML模板,这块模板显示不显示.以及怎样显示由父组件来决定. 插槽模板是slot,它是一个空壳子,因为它显示与隐藏以及最后用什么样的html模板显示由父组件控制.但 ...

  9. EasyCode Entity 实体类模板 IDEA

    自己修改了一份EasyCode的实体类模板,防止日后找不到在这里存一下 修改了如下内容: 取消生成GetSet方法,改用Lombok 修改默认命名规则,改为[表名Entity.java] 取消了实现序 ...

  10. 对DiscuzQ的一些使用见解

    之前因为体验了DiscuzQ,在几番纠结后,把博客换成了DiscuzQ(以下简称DZQ). 在一个月的使用中,发现这个程序对于个人来说,十分不友好. 于是今天又换回了Wordpress. 在这里说一下 ...