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'>)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程宝库

 解读pandas.DataFrame.corrwithpandas.DataFrame.corrwith用于计算DataFrame中行与行或者列与列之间的相关性。Param ...