Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)

时间:2022-01-01 14:32:49

Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)

 

by:授客 QQ1033553122

 

测试环境:

win7 64位

Python版本:Python 3.3.5

代码实践:

1、在子类中通过“类名”调用父类的方法

class FatherA:

def __init__(self):

print('init action in father class A')

class SubClassB(FatherA):

def __init__(self):

print('init action in subclass B')

FatherA.__init__(self) # 在子类中调用父类的方法:父类名.方法名称(参数)

if __name__ == '__main__':

b = SubClassB()

运行结果:

>>> ================================ RESTART ================================

>>>

init action in subclass B

init action in father class A

缺点:当一个子类的父类发生变化时(如类SubClassB的父类由FatherA变为FatherD时),必须遍历整个类定义,把子类中所有的父类类名全部替换过来

2、在子类中通过“super”方法调用父类的方法

场景1、单层继承

class FatherA:

def __init__(self):

print('init action in father class A')

class SubClassB(FatherA):

def __init__(self):

print('init action in father class B')

super().__init__() # 在子类中调用父类的方法:super().方法名称(参数)

if __name__ == '__main__':

b = SubClassB()

class FatherA:

def __init__(self):

print('init action in father class A')

class SubClassB(FatherA):

def __init__(self):

print('init action in subclass B')

super(SubClassB, self).__init__()  # 在子类中调用父类的方法:super(type, obj).方法名称(参数)

if __name__ == '__main__':

b = SubClassB()

运行结果:

>>> ================================ RESTART ================================

>>>

init action in father class B

init action in father class A

说明:

1、super(type, obj),其中obj必须是type类型、type子类类型的实例,否则会报错:

TypeError: super(type, obj): obj must be an instance or subtype of type

2、super().__init__()  效果等同 super(SubClassB, self). __init__()

场景2、多层继承

实验1

class FatherA:

def __init__(self):

print('init action in father class A')

class SubClassB(FatherA):

def __init__(self):

print('init action in subclass B')

super().__init__()

class SubClassC(SubClassB):

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 subclass B

init action in father class A

对比实验1-1

class FatherA:

def __init__(self):

print('init action in father class A')

class SubClassB(FatherA):

def __init__(self):

print('init action in subclass B')

super().__init__()

class SubClassC(SubClassB):

def __init__(self):

print('init action in subclass C')

super(SubClassB, self).__init__()

if __name__ == '__main__':

b = SubClassC()

运行结果:

>>> ================================ RESTART ================================

>>>

init action in subclass C

init action in father class A

>>>

对比实验1-2

class FatherA:

def __init__(self):

print('init action in father class A')

class SubClassB(FatherA):

def __init__(self):

print('init action in subclass B')

super().__init__()

class SubClassC(SubClassB):

def __init__(self):

print('init action in subclass C')

super(FatherA, FatherA).__init__(self) # 在子类中调用父类的方法:super(type, type).方法名称(参数)

if __name__ == '__main__':

b = SubClassC()

说明:可把 super(FatherA, FatherA).__init__(self) 换成:

super(FatherA, self).__init__()

super(FatherA, SubClassB).__init__(self)

super(FatherA, SubClassC).__init__(self)

注意:以上这种情况,必须给__init__传递参数self,否则会报错:

TypeError: __init__() missing 1 required positional argument: 'self'

运行结果:

>>> ================================ RESTART ================================

>>>

init action in subclass C

>>>

说明:super(type1, type2) ,type2必须和type1相同类型,或者其子类类型

实验2

class FatherA:

def __init__(self):

print('init action in father class A')

class SubClassB(FatherA):

def __init__(self):

print('init action in subclass B')

super().__init__()

class SubClassC(SubClassB):

def __init__(self):

print('init action in subclass C')

super().__init__()

class SubClassD(SubClassC):

def __init__(self):

print('init action in subclass D')

super().__init__()

if __name__ == '__main__':

b = SubClassD()

运行结果:

>>> ================================ RESTART ================================

>>>

init action in subclass D

init action in subclass C

init action in subclass B

init action in father class A

对比实验2-1

class FatherA:

def __init__(self):

print('init action in father class A')

class SubClassB(FatherA):

def __init__(self):

print('init action in subclass B')

super().__init__()

class SubClassC(SubClassB):

def __init__(self):

print('init action in subclass C')

super().__init__()

class SubClassD(SubClassC):

def __init__(self):

print('init action in subclass D')

super(SubClassB, self).__init__()

if __name__ == '__main__':

b = SubClassD()

运行结果:

>>> ================================ RESTART ================================

>>>

init action in subclass D

init action in father class A

>>>

对比实验2-2

class FatherA:

def __init__(self):

print('init action in father class A')

class SubClassB(FatherA):

def __init__(self):

print('init action in subclass B')

super().__init__()

class SubClassC(SubClassB):

def __init__(self):

print('init action in subclass C')

super().__init__()

class SubClassD(SubClassC):

def __init__(self):

print('init action in subclass D')

super(SubClassC, self).__init__()

if __name__ == '__main__':

b = SubClassD()

运行结果:

>>> ================================ RESTART ================================

>>>

init action in subclass D

init action in subclass B

init action in father class A

通过对比实验2-1, 2-2,可看出super(type[,type2_or_obj]),type决定了super调用方法所在的父类--type的父类(如果有的话),即type决定了前往哪个父类调用指定的方法

场景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

>>>