菱形继承
菱形继承是指在一个类继承体系中存在一条继承链上的两个不同类,它们最终都直接或间接地继承自同一个父类。
"""
Human
Man Woman
Children
"""
class Human():
pty = 1
def feel(self):
print("1")
print(self.pty)
print("2")
class Man(Human):
def feel(self):
print("3")
print(super())
super().feel()
print("4")
class Woman(Human):
def feel(self):
print("5")
print(super())
super().feel()
print("6")
class Children(Man,Woman):
def feel(self):
print("7")
print(super())
super().feel()
print("8")
son = Children()
son.feel()
# 打印顺序为73512648
这个代码中,在调用son的feel方法之后,先打印7然后进入父类的feel中,因为继承的第一个父类是Man,所以会打印3,然后在进入Woman中的feel,打印5,在进入Human中的feel。打印1,直到执行完Human中的feel,回到Woman,继续执行,在回到Man,最后是Children