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为父类,AppleOrange为子类,子类继承了父类的特性,因此AppleOrange也拥有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'>)
  • 106
    点赞
  • 404
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值