Python类继承及super()函数使用说明

 更新时间:2022年11月24日 09:12:45   作者:waifdzdn  
这篇文章主要介绍了Python类继承及super()函数使用说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Python中单类继承

Python是一门面向对象的编程语言,支持类继承。

新的类称为子类(Subclass),被继承的类称为父类、基类或者超类。子类继承父类后,就拥有父类的所有特性。

类继承的简单例子:

普通类方法继承

class Fruit():
    def color(self):
        print("colorful")

class Apple(Fruit):
    pass

class Orange(Fruit):
    pass

apple = 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 = color
        self.shape = shape

class Apple(Fruit):
    def __init__(self, color, shape, taste):
        Fruit.__init__(self, color, shape) # 等价于super().__init__(color, shape)
        self.taste = taste
    
    def 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 = taste
    
    def 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语言的优势是什么

    python语言的优势是什么

    这篇文章主要介绍了python语言的优势是什么,从各个方面做了分析,需要的朋友们可以参考下
    2020-06-06
  • django美化后台django-suit的安装配置操作

    django美化后台django-suit的安装配置操作

    这篇文章主要介绍了django美化后台django-suit的安装配置操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • 详解Python中的null是什么

    详解Python中的null是什么

    这篇文章主要介绍了Python中的null是什么,Python中其实没有null这个词,取而代之的是None对象,即特殊类型NoneType,代表空、没有,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • Python的动态重新封装的教程

    Python的动态重新封装的教程

    这篇文章主要介绍了Python的动态重新封装的教程,本文来自于IBM的官方开发者文档,需要的朋友可以参考下
    2015-04-04
  • Python数据分析之双色球统计两个红和蓝球哪组合比例高的方法

    Python数据分析之双色球统计两个红和蓝球哪组合比例高的方法

    这篇文章主要介绍了Python数据分析之双色球统计两个红和蓝球哪组合比例高的方法,涉及Python数值运算及图形绘制相关操作技巧,需要的朋友可以参考下
    2018-02-02
  • 详解python读写json文件

    详解python读写json文件

    这篇文章主要为大家介绍了python读写json文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-12-12
  • 通过pycharm使用git的步骤(图文详解)

    通过pycharm使用git的步骤(图文详解)

    这篇文章主要介绍了通过pycharm使用git的步骤(图文详解),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • Python机器学习利用鸢尾花数据绘制ROC和AUC曲线

    Python机器学习利用鸢尾花数据绘制ROC和AUC曲线

    这篇文章主要为大家介绍了Python机器学习利用鸢尾花数据绘制ROC和AUC曲线实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • python中使用websocket方法实例详解

    python中使用websocket方法实例详解

    WebSocket是一种网络通信协议,它在单个TCP连接上提供全双工的通信信道,本文我们将探讨如何在Python中使用WebSocket实现实时通信,感兴趣的朋友跟随小编一起看看吧
    2024-03-03
  • Python中变量的输入输出实例代码详解

    Python中变量的输入输出实例代码详解

    这篇文章主要介绍了Python中变量的输入输出问题,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2019-07-07

最新评论