场景3、多重继承
实验1:
class FatherA:
def __init__(self):
print('init action in father class A')
class FatherB:
def __init__(self):
print('init action in father class B')
class SubClassC(FatherA, FatherB):
def __init__(self):
print('init action in subclass C')
super(FatherB).__init__()
if __name__ == '__main__':
b = SubClassC()
运行结果:
>>> ================================ RESTART ================================
>>>
init action in subclass C
对比实验1-1:
class FatherA:
def __init__(self):
print('init action in father class A')
class FatherB:
def __init__(self):
print('init action in father class B')
class SubClassC(FatherB, FatherA):
def __init__(self):
print('init action in subclass C')
super().__init__()
if __name__ == '__main__':
b = SubClassC()
运行结果:
>>> ================================ RESTART ================================
>>>
init action in subclass C
init action in father class B
>>>
对比实验1-2:
class FatherA:
def __init__(self):
print('init action in father class A')
class FatherB:
def __init__(self):
print('init action in father class B')
class SubClassC(FatherA, FatherB):
def __init__(self):
print('init action in subclass C')
super().__init__()
if __name__ == '__main__':
b = SubClassC()
运行结果:
>>> ================================ RESTART ================================
>>>
init action in subclass C
init action in father class A
>>>
对比实验1-3:
class FatherA:
def __init__(self):
print('init action in father class A')
class FatherB:
def __init__(self):
print('init action in father class B')
class SubClassC(FatherA, FatherB):
def __init__(self):
print('init action in subclass C')
super(FatherB).__init__()
if __name__ == '__main__':
b = SubClassC()
>>> ================================ RESTART ================================
>>>
init action in subclass C
对比实验1-4:
class FatherA:
def __init__(self):
print('init action in father class A')
def testfn(self, arg):
print('testfn in father class A')
class FatherB:
def __init__(self):
print('init action in father class B')
def testfn(self):
print('testfn in father class B')
class SubClassC(FatherA, FatherB):
def __init__(self):
print('init action in subclass C')
super().testfn()
if __name__ == '__main__':
b = SubClassC()
运行结果:
>>> ================================ RESTART ================================
>>>
init action in subclass C
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/1.py", line 21, in
b = SubClassC()
File "C:/Users/Administrator/Desktop/1.py", line 18, in __init__
super().testfn()
TypeError: testfn() missing 1 required positional argument: 'arg'
>>>
对比实验1-5:
class FatherA:
def __init__(self):
print('init action in father class A')
def testfn(self):
print('testfn in father class A')
class FatherB:
def __init__(self):
print('init action in father class B')
def testfn(self, arg):
print('testfn in father class B')
class SubClassC(FatherA, FatherB):
def __init__(self):
print('init action in subclass C')
super().testfn()
if __name__ == '__main__':
b = SubClassC()
运行结果:
>>> ================================ RESTART ================================
>>>
init action in subclass C
testfn in father class A
说明:通过对比实验1-1,1-2,1-3,1-4,1-5可以看出,子类水平方向上,继承多个父类,以super().method(参数)方法调用父类的方法,如果不同父类中存在同名方法method(不管参数列表是否相同),则按继承顺序,选择第一个父类中的方法。,如果想要调用多个方法咋办?如下,通过类名调用
class FatherA:
def __init__(self):
print('init action in father class A')
class FatherB:
def __init__(self):
print('init action in father class B')
class SubClassC(FatherA, FatherB):
def __init__(self):
print('init action in subclass C')
FatherA.__init__(self)
FatherB.__init__(self)
if __name__ == '__main__':
b = SubClassC()
>>> ================================ RESTART ================================
>>>
init action in subclass C
init action in father class A
init action in father class B
>>>