多继承
class A: def ces(self): print('a-ces') class B(A): def ces(self): print('b-ces') class C(A): def ces(self): print('c-ces') class D(B, C): def ces(self): super(B, self).ces() # c-ces super(C, self).ces() # a-ces super(D, self).ces() # b-ces super().ces() # b-ces 相当于本身 super(D, self).ces() print('d-ces') d = D() print(d.__class__.mro()) print(d.ces()) >>> [<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>
, <class 'object'>] b-ces b-ces d-ces None
私有变量: 继承时类的私有变量不会被继承
class Animal: # 类属性 (静态字段) eye = 2 _age = 18 __leg = 14 def __init__(self, name): self.name = name def run(self): print(Animal.__leg) class Dog(Animal): # 继承后调用私有属性 def fn(self):
print(Animal._age) # 但下划线调用 print(Animal._Animal__leg) # 双
私有方法通过间接的方法调用私有方法:
class Animal: # 类属性 (静态字段) eye = 2 _age = 18 __leg = 14 # 普通方法间接调用私有方法 def test(self): self.__pwd() def __pwd(self): print('pwd')