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的超类 基类 父类 # ...
随机推荐
- 【设计模式】- 生成器模式(Builder)
生成器模式 建造者模式.Builder 生成器模式 也叫建造者模式,可以理解成可以分步骤创建一个复杂的对象.在该模式中允许你使用相同的创建代码生成不同类型和形式的对象. 生成器的结构模式 生成器(Bu ...
- Spring Boot 2.3 新特配置文件属性跟踪
背景 当我们使用 spring boot 在多环境打包,配置属性在不同环境的值不同,如下: spring: profiles: active: @project.profile@ #根据maven 动 ...
- 【项目】手写FTP服务器-C++实现FTP服务器
X_FTP_server 手写FTP服务器-C++实现FTP服务器 项目Gitee链接:https://gitee.com/hsby/ftp_Server 简介 一个基于libevent的高并发FTP ...
- 005-Java中的控制语句
目录 一.控制语句 一.作用 二.分类 二.选择语句(分支语句) 一.if 语句 二.switch语句 三.循环语句 一.for循环 二.while循环(while循环的循环次数是:0~n次) 三.d ...
- C++ new和delete运算符得简单使用
NEW C++ 中的new运算符用来分配内存,和c语言中得malloc有相似得功能. 使用new为当个元素开辟内存空间,并返回地址 typeName *pointer_name =new typeNa ...
- 做个开源博客学习Vite2 + Vue3 (四)实现博客功能
我们再来看一下管理类的设计. Composition API,就是组合API的意思,那么是不是应该把js代码分离出来,做成独立的管理类的形式呢? 这样代码可以更整洁一些,主要是setup里面的代码就不 ...
- yolov2算法浅见
因为最近在复习yolo系列的算法,就借着这个机会总结一下自己对这个算法的理解,由于是第一次写算法类的博客,文中有什么错误和行文不通的地方还希望大家指正. yolov2与yolov1有很多改变. 最重要 ...
- Alpine镜像
Alpine Linux 是一个面向安全,轻量级的基于musl libc与busybox项目的Linux发行版. Alpine 提供了自己的包管理工具 apk,可以通过 https://pkgs.al ...
- 0704-使用GPU加速_cuda
0704-使用GPU加速_cuda 目录 一.CPU 和 GPU 数据相互转换 二.使用 GPU 的注意事项 三.设置默认 GPU 四.GPU 之间的切换 pytorch完整教程目录:https:// ...
- POJ2983 查分约束系统
题意: 给你n个点,然后给你两种情况,P a b c,表明a在b的北边c那么远,V a b 表明a在b的北边(距离最少是1),问你这些条件是否冲突. 思路: 一开始想用带权并 ...