请帮助我在Python中理解多重继承的概念(我来自C#背景,不支持多重继承。
我正在使用Python 3.7.6。
在下面的代码中,类Apple
继承了类ToyFruit
,NaturalFruit
,FakeFruit
和RoboticFruit
。尽管ToyFruit
,NaturalFruit
和FakeFruit
继承了Fruit
基类,但RoboticFruit
具有不同的BaseClass
Robot
。
我注意到RoboticFruit
和Robot
根本没有被调用。
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):
然后根本不调用ToyFruit
,NaturalFruit
,FakeFruit
和Fruit
__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'>
)
因此,我想不调用RoboticFruit
和Robot
,因为在类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")
我不确定它是否返回您想要的结果,但是可以用硬编码调用将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