文章目录

  • Python中单类继承
    • 普通类方法继承
    • 初始化函数继承
  • Python中多类继承

Python中单类继承

Python是一门面向对象的编程语言,支持类继承。新的类称为子类(Subclass),被继承的类称为父类、基类或者超类。子类继承父类后,就拥有父类的所有特性。类继承的简单例子:

普通类方法继承

class Fruit():def color(self):print("colorful")class Apple(Fruit):passclass Orange(Fruit):passapple = Apple()
orange = Orange()
apple.color()
orange.color()# 输出
# colorful
# colorful

这里Fruit为父类,Apple和Orange为子类,子类继承了父类的特性,因此Apple和Orange也拥有Color方法。
子类除了可以继承父类的方法,还可以覆盖父类的方法:

class Fruit():def color(self):print("colorful")class Apple(Fruit):def color(self):print("red")class Orange(Fruit):def color(self):print("orange")apple = Apple()
orange = Orange()
apple.color()
orange.color()# 输出
# red
# orange

子类可以在继承父类方法的同时,对方法进行重构。这样一来,子类的方法既包含父类方法的特性,同时也包含子类自己的特性:

class Fruit():def color(self):print("Fruits are colorful")class Apple(Fruit):def color(self):super().color()print("Apple is red")class Orange(Fruit):def color(self):super().color()print("Orange is orange")apple = Apple()
orange = Orange()
apple.color()
orange.color()# 输出
# Fruits are colorful
# Apple is red
# Fruits are colorful
# Orange is orange

初始化函数继承

如果我们需要给类传入参数,需要使用初始化函数。如果所有子类中部分参数是相同的,那么可以在父类的初始化函数中定义这些参数,然后子类继承父类的初始化函数,这样所有子类就可共享这些参数,而不需要在每个子类中单独定义。初始化函数的继承:

class Fruit():def __init__(self, color, shape):self.color = colorself.shape = shapeclass Apple(Fruit):def __init__(self, color, shape, taste):Fruit.__init__(self, color, shape) # 等价于super().__init__(color, shape)self.taste = tastedef feature(self):print("Apple's color is {}, shape is {} and taste {}".format(self.color, self.shape, self.taste))class Orange(Fruit):def __init__(self, color, shape, taste):Fruit.__init__(self, color, shape)self.taste = tastedef feature(self):print("Orange's color is {}, shape is {} and taste {}".format(self.color, self.shape, self.taste))apple = Apple("red", "square", "sour")
orange = Orange("orange", "round", "sweet")
apple.feature()
orange.feature()# 输出
# Apple's color is red, shape is square and taste sour
# Orange's color is orange, shape is round and taste sweet

Python中多类继承

在单类继承中,super()函数用于指向要继承的父类,且不需要显式的写出父类名称。但是在多类继承中,会涉及到查找顺序(MRO)、钻石继承等问题。MRO 是类的方法解析顺序表, 也就是继承父类方法时的顺序表。钻石继承:

    A/ \B   C\ /D

如图所示,A是父类,B和C继承A,D继承B和C。下面举例说明钻石继承的继承顺序:

class Plant():def __init__(self):print("Enter plant")print("Leave plant")class Fruit(Plant):def __init__(self):print("Enter Fruit")super().__init__()print("Leave Fruit")class Vegetable(Plant):def __init__(self):print("Enter vegetable")super().__init__()print("Leave vegetable")class Tomato(Fruit, Vegetable):def __init__(self):print("Enter Tomato")super().__init__()print("Leave Tomato")tomato = Tomato()
print(Tomato.__mro__)# 输出
# Enter Tomato
# Enter Fruit
# Enter vegetable
# Enter plant
# Leave plant
# Leave vegetable
# Leave Fruit
# Leave Tomato
# (<class '__main__.Tomato'>, <class '__main__.Fruit'>, <class '__main__.Vegetable'>, <class '__main__.Plant'>, <class 'object'>)

Python类继承及super()函数相关推荐

  1. Python类继承之虚函数

    虚函数在C++里面广泛使用,也为后面派生的一种独特类型interface奠定了编程界的基础,在python中也有类似的概念(抽象类),使用如下: 抽象类可以继承abc,抽象接口使用@abstractm ...

  2. python类继承实例_Python实现类继承实例

    Python是一种解释型.面向对象.动态数据类型的高级程序设计语言,本文就举一例Python类继承的实例. 实例代码如下: #! /usr/bin/python # Filename: inherit ...

  3. python类继承实例_python类继承与子类实例初始化用法分析

    这篇文章主要介绍了python类继承与子类实例初始化用法,实例分析了Python类的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下 [ 先贴参考书籍原文(中文英文对照)] __init__方法介 ...

  4. python类继承的重写和super

    给已经存在的类添加新的行为,继承是非常好的实现方式.但是如果要改变行为呢?比如在Python继承扩展内置类,我们的contact类只允许一个名字和一个邮箱,但是如果要对某些人增加电话号码呢?这里可以通 ...

  5. Python 类继承,__bases__, __mro__, super

    Python是面向对象的编程语言,也支持类继承. >>> class Base: ... pass ... >>> class Derived(Base): ... ...

  6. python类继承重复_python单例模式,可以继承,不会重复执行初始化函数的版本

    网上最长见的版本: 1 classSingleton(object):2 __instance=None3 def__init__(self):4 pass 5 def__new__(cls,*arg ...

  7. python可以继承多个类吗_python类继承(super多类继承)

    1. python2和python3中定义类的形式不同 python3中只有只有新式类 class A(object): # python3中定义新式类 class A: # python3中定义新式 ...

  8. 【python】类继承中super的用法

    阅读本文,需要知道python中的类与继承的概念. 1.总述 在python中,通过类的继承机制,可以实现在子类中调用父类的方法,从而避免写重复的代码.但在面临多继承时,如果多个父类中都实现了某个相同 ...

  9. python 多继承与super使用详解_继承中的MRO与super详解

    Python进阶-继承中的MRO与super 写在前面如非特别说明,下文均基于Python3 摘要 本文讲述Python继承关系中如何通过super()调用"父类"方法,super ...

最新文章

  1. SpringMVC中实现的token,防表单重复提交
  2. 单位四元数(unit quaternion)
  3. twitter storm学习 - 安装部署问题汇总
  4. 一款不错的基于WEB技术的文件服务器
  5. AUC 评价指标详解,准确率(ACC),敏感性(sensitivity),特异性(specificity)计算 Python3【TensorFlow2入门手册】
  6. YYModel 源码解读(二)之YYClassInfo.h (1)
  7. 1581: 统计成绩-一题简单的坑题
  8. 兼容性好的overflow CSS清除浮动一例
  9. educoder 使用线程锁(lock)实现线程同步_线程间的通信(一)
  10. WINDOWS系统Eclipse+NDK+Android + OpenCv
  11. 网络数据库的复制和同步(转摘)
  12. 回文数(信息学奥赛一本通-T1309)
  13. 简单脚本之显示系统当前的一些信息
  14. iOS_SN_详解沙河(转载)
  15. 一名 IT 经理是如何把项目带崩的?
  16. Struts2的常见的配置文件介绍
  17. R语言:使用ComplexHeatmap包绘制热图
  18. 掌门教育市值再创新低:集体诉讼风险未解,近万名教师“无证”
  19. DOS命令格式化制作U盘
  20. 【linux】循序渐进学运维-printf

热门文章

  1. ajax 文本框 搜索,JQuery+AJAX实现搜索文本框的输入提示功能
  2. 人脸形变算法——液化变形
  3. 基于Labview的图像傅里叶变换研究-含Labview程序
  4. UnityWebRequest 加载Texture2D
  5. 移动硬盘上安装WIN PE最简单的方法-转贴
  6. python批量修改图像像素,修改命名,二值化,划分数据集
  7. 什么是 geobuf?
  8. SQL中字符截取、拼接 、转换常用方法
  9. com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
  10. 数据库中QuerySet API