请在这里用Python帮助我理解多重继承的概念(我来自C#背景,它不支持多重继承)。

我正在使用Python 3.7.6。

在下面的代码中,类Apple继承了类ToyFruitNaturalFruitFakeFruitRoboticFruitToyFruitNaturalFruitFakeFruit继承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()委托给方法解析顺序(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)的前一个。如果将呼叫添加到super()中的Fruit,它将正常工作:

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

07-25 21:54