8.多层多继承时的初始化问题
菱形继承
class Person(object): def __init__(self,aaa): print("Person") self.aaa = aaa class Father(Person): def __init__(self,aaa,name): Person.__init__(self,aaa) print("Father") self.name = name class Mother(Person): def __init__(self,aaa,age): Person.__init__(self, aaa) print("Mother") self.age = age class Son(Father,Mother): def __init__(self,aaa,name,age,gender): print("Son") Mother.__init__(self,aaa,age) Father.__init__(self,aaa,name) self.gender = gender s = Son(1,'tom',12,'男') print("______________________________________________________") print(s.aaa) print(s.name) print(s.age) print(s.gender)
菱形继承的问题如何解决呢?
super解决
让子类调用father,father调用mather,mather调用person,变成线性的
class Person(object): def __init__(self,aaa): print("Person") self.aaa = aaa class Father(Person): def __init__(self,aaa,name,age): super(Father,self).__init__(aaa,age) print("Father") self.name = name class Mother(Person): def __init__(self,aaa,age): super(Mother,self).__init__(aaa) print("Mother") self.age = age class Son(Father,Mother): def __init__(self,aaa,name,age,gender): print("Son") super(Son,self).__init__(aaa,name,age) self.gender = gender s = Son(1,'tom',12,'男') print("______________________________________________________") print(s.aaa) print(s.name) print(s.age) print(s.gender)
super的执行过程
mro是个元组,元素的顺序是解释器定义的
注意根据这个列表我们可以得到son——father——mather——person这个顺序
如果顺序不对也会报错
class Person(object): def __init__(self,aaa): print("Person") self.aaa = aaa class Father(Person): def __init__(self,aaa,name): super(Father,self).__init__(aaa) print("Father") self.name = name class Mother(Person): def __init__(self,aaa,name,age): super(Mother,self).__init__(aaa,name) print("Mother") self.age = age class Son(Father,Mother): def __init__(self,aaa,name,age,gender): print("Son") super(Son,self).__init__(aaa,name,age) self.gender = gender s = Son(1,'tom',12,'男') print(Son.__mro__)
9.多继承初始化传参问题
class Person(object): def __init__(self,aaa): print("Person") self.aaa = aaa class Father(Person): def __init__(self,name,*args): #这里不用担心传参的问题,因为这里会自动解包 super(Father,self).__init__(*args) print("Father") self.name = name class Mother(Person): def __init__(self,age,aaa): super(Mother,self).__init__(aaa) print("Mother") self.age = age class Son(Father,Mother): def __init__(self,gender,name,age,aaa): print("Son") super(Son,self).__init__(name,age,aaa) self.gender = gender s = Son('男','tom',12,1) print(Son.__mro__) print(s.name) print(s.age) print(s.gender) print(s.aaa)
10.super简化写法
class Person(object): def __init__(self,aaa): print("Person") self.aaa = aaa class Father(Person): def __init__(self,name,*args): #这里不用担心传参的问题,因为这里会自动解包 #super(Father,self).__init__(*args) super().__init__(*args) print("Father") self.name = name class Mother(Person): def __init__(self,age,aaa): #super(Mother,self).__init__(aaa) super().__init__(aaa) print("Mother") self.age = age class Son(Father,Mother): def __init__(self,gender,name,age,aaa): print("Son") #super(Son,self).__init__(name,age,aaa) super().__init__(name, age, aaa) self.gender = gender s = Son('男','tom',12,1) print(Son.__mro__) print(s.name) print(s.age) print(s.gender) print(s.aaa)
11.影响mro的顺序
类的额继承书写顺序会影响mro的顺序,但不会改变mro的顺序
''' 多重多继承时,方法的查找顺序也参考MRO ''' class A(object): pass class B(A): pass class C(A): pass class D(B,C): pass print(D.__mro__)
''' 多重多继承时,方法的查找顺序也参考MRO ''' class A(object): pass class B(A): pass class C(A): pass class D(C,B): pass print(D.__mro__)
最子类的参数书写顺序会影响mro的元素顺序
class A(object): def show(self): print('A show run ...') class B(A): def show(self): print('B show run ...') class C(A): def show(self): print('C show run ...') class D(C,B): pass print(D.__mro__) D().show()
class A(object): def show(self): print('A show run ...') class B(A): def show(self): print('B show run ...') class C(A): def show(self): print('C show run ...') class D(B,C): pass print(D.__mro__) D().show()
12.调用父类两种方法
super调用
class A(object): def show(self): print('A show run ...') class B(A): def show(self): print('B show run ...') class C(A): def show(self): super().show() print('C show run ...') class D(C,B): pass print(D.__mro__) D().show()
直接调用
class A(object): def show(self): print('A show run ...') class B(A): def show(self): print('B show run ...') class C(A): def show(self): B().show() print('C show run ...') class D(C,B): pass print(D.__mro__) D().show()
或者
class A(object): def show(self): print('A show run ...') class B(A): def show(self): print('B show run ...') class C(A): def show(self): B.show(self) print('C show run ...') class D(C,B): pass print(D.__mro__) D().show()
因为B.show()的B不是一个对象实例
结语
上班时间不多,尽量多更新,点点赞吧!