python - 具有多个基类的多重继承

标签 python python-3.x multiple-inheritance object-oriented-analysis

请帮我理解Python中多重继承的概念(我来自C#背景,不支持多重继承)。

我使用的是Python 3.7.6

在下面的代码中,类 Apple 继承了类 ToyFruitNaturalFruitFakeFruit >机器人水果。虽然 ToyFruitNaturalFruitFakeFruit 继承 Fruit 基类,但 RoboticFruit 有一个不同的BaseClass Robot

我注意到 RoboticFruitRobot 根本没有被调用。

class Fruit:
    def __init__(self, name):
        print("This is the Fruit __init__ function")
        self.test = "BaseClass"
        self.name = name
        print("Fruit object created")


class NaturalFruit(Fruit):
    def __init__(self, name):
        print("This is the NaturalFruit __init__ function")
        super().__init__(name)
        self.type = "Natural"
        print("This is a Natural Fruit")
        self.test = "NaturalClass"
        print("Natural Fruit object created")


class FakeFruit(Fruit):
    def __init__(self, name):
        print("This is the FakeFruit __init__ function")
        super().__init__(name)
        self.type = "Fake"
        print("This is a Fake Fruit")
        self.test = "FakeClass"
        print("Fake Fruit object created")


class ToyFruit(Fruit):
    def __init__(self, name):
        print("This is the ToyFruit __init__ function")
        super().__init__(name)
        self.type = "Toy"
        print("This is the Toy Fruit")
        self.test = "ToyClass"
        print("Toy Fruit object created")


class Robot:
    def __init__(self, name):
        print("This is the ROBOT __init__ function")
        self.test = "RobotClass"
        self.name = name
        print("Robot object created")


class RoboticFruit(Robot):
    def __init__(self, name):
        super().__init__("RoboticFruit")
        print("Robotic Fruit")


class Apple(ToyFruit, NaturalFruit, FakeFruit, RoboticFruit):
    def __init__(self):
        super().__init__("Apple")
        print("Apple object created")


apple = Apple()
# print(apple.name)
print(apple.test)

输出:-

This is the ToyFruit __init__ function
This is the NaturalFruit __init__ function
This is the FakeFruit __init__ function
This is the Fruit __init__ function
Fruit object created
This is a Fake Fruit
Fake Fruit object created
This is a Natural Fruit
Natural Fruit object created
This is the Toy Fruit
Toy Fruit object created
Apple object created
ToyClass

如果我将订单交换为

class Apple(RoboticFruit, ToyFruit, NaturalFruit, FakeFruit):

然后,ToyFruitNaturalFruitFakeFruitFruit __init__ 方法是根本没有被调用。 我不明白为什么 RoboticFruit 类构造函数被跳过。

最佳答案

在多重继承的情况下,super() 委托(delegate)给方法解析顺序 (MRO) 中的下一个对象。我们可以看到Apple类的MRO:

print(Apple.__mro__)

# output:
(
<class '__main__.Apple'>, 
<class '__main__.ToyFruit'>, 
<class '__main__.NaturalFruit'>, 
<class '__main__.FakeFruit'>, 
<class '__main__.Fruit'>, 
<class '__main__.RoboticFruit'>, 
<class '__main__.Robot'>, 
<class 'object'>
)

所以我认为 RoboticFruitRobot 没有被调用,因为类中没有像 super().__init__(name) 这样的调用Fruit,这是该顺序中 RoboticFruit 的前一个 (MRO)。如果您在 Fruit 中添加对 super() 的调用,它应该可以正常工作:

class Fruit:
    def __init__(self, name):
        print("This is the Fruit __init__ function")
        super().__init__(name)
        self.test = "BaseClass"
        self.name = name
        print("Fruit object created")

关于python - 具有多个基类的多重继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59907412/

相关文章:

python - 如何将带有时区字符串的字符串转换为日期时间?

python - 更新条目中的文本 (Tkinter)

python - 更好地说明闭包?

c++ - C++ 如何选择调用哪个重载函数?

python - 有没有一种简单的方法可以以树形式打印类的层次结构?

来自多个类的 C++ 多态性

python - 将具有索引差异的特征添加到带有条件的下一行

python - 如何使 UUID PK 更易于索引

python - 将 GMT 时间戳转换为 Pandas 中的时间戳

python 3 : TypeError: 'int' object is not callable