具有多个基类的多重继承

问题描述 投票:1回答:2

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

我正在使用Python 3.7.6

在下面的代码中,类Apple继承了类ToyFruitNaturalFruitFakeFruitRoboticFruit。尽管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类构造函数被跳过。

python python-3.x multiple-inheritance object-oriented-analysis
2个回答
2
投票

[如果是多重继承,则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这样的调用,该调用是按该顺序(MRO)到RoboticFruit的前一个。如果您向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")

0
投票

我不确定它是否返回您想要的结果,但是可以用硬编码调用将super()调用替换为__init__

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")
        Fruit.__init__(self, 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")
        Fruit.__init__(self, 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")
        Fruit.__init__(self, 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):
        Robot.__init__(self, "RoboticFruit")
        print("Robotic Fruit")


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


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

# output:
# This is the ToyFruit __init__ function
# This is the Fruit __init__ function
# Fruit object created
# This is the Toy Fruit
# Toy Fruit object created
# This is the NaturalFruit __init__ function
# This is the Fruit __init__ function
# Fruit object created
# This is a Natural Fruit
# Natural Fruit object created
# 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 the ROBOT __init__ function
# Robot object created
# Robotic Fruit
# Apple object created
# RobotClass
© www.soinside.com 2019 - 2024. All rights reserved.