一 继承的概念

种类

  • 单继承
  • 多继承

至少两个类:

  • 子类:一个类继承另外一个类,那么该类是子类(也叫作衍生类)
  • 父类:另外一个,这个被继承的类,叫做父类(也叫作超类),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. windows上phpstudy配置memcache

    原文   http://blog.csdn.net/ltx06/article/details/78588448   总的来说,分两步:同时安装memcached软件服务和安装php_memcache ...

  2. 动态语言 VS 静态语言

    静态语言 VS 动态语言 动态语言 是一类在运行时可以改变其结构的语言:例如新的函数.对象.甚至代码可以被引进,已有的函数可以被删除或是其他结构上的变化.通俗点说就是在运行时代码可以根据某些条件改变自 ...

  3. linux中[gcc -shared -fPIC]的含义

    linux在gcc编译时加上 -shared 参数时,目的是使源码编译成动态库 .so 文件: 而-fPIC的作用是 告知编译器 生成位置无关代码(编译产生的代码没有绝对位置,只有相对位置):从而可以 ...

  4. Day14_81_反射机制获取Class属性

    反射机制获取Class属性 获取属性 方法一: Class对象 . getFields();只能用来获取公开的属性,不能获取有私有的或者受保护的属性 获取属性 方法二: Class对象 . getDe ...

  5. Leecode第二题:两数相加

    Leecode2 先看题目 : 给你两个 非空 的链表,表示两个非负的整数.它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字. 请你将两个数相加,并以相同形式返回一个表示和的 ...

  6. 1019 General Palindromic Number

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  7. 【CompletableFuture】CompletableFuture测试runAsync()方法调用

    问题 CompletableFuture.runAsync() 返回 CompletableFuture<Void>对象,调用CompletableFuture.allOf(f1,f2). ...

  8. Xposed学习三:基石

    在上一篇我们留下问题:handleLoadPackage如何生效即在何时被执行. 先看XposedBridge.class的main(该函数是在appruntime.start函数中替换原先zygot ...

  9. 11.PHP与MySQL

    PHP与MySQL 首先是PHPStorm设置创建SQL的教程,找到了一个写的不错的,在这里:http://blog.csdn.net/knight_quan/article/details/5198 ...

  10. 简化mapstruct代码: mapstruct-spring-plus

    mapstruct MapStruct 是一个属性映射工具,只需要定义一个 Mapper 接口,MapStruct 就会自动实现这个映射接口,避免了复杂繁琐的映射实现.MapStruct官网地址: h ...