类的继承
# # python3默认是广度优先,先找自己的方法或属性,如果没,往父类找 # class Animal: # def __init__(self): # print('执行Animal.__init__') # self.func() # # def eat(self): # print('%s eating' %self.name) # # def drink(self): # print('%s drinking' % self.name) # # def func(self): # print('Animal.func') # # class Dog(Animal): # def guard(self): # print('guarding') # # def func(self): # 重写父类方法,派生方法 # print('Dog.func') # # class Bird(Animal): # def __init__(self, name): # # Animal.__init__(self) # 父类名.方法名 需要自己传self参数 # super().__init__() # super().方法名 不需要自己传self,这是新式类 # self.name = name # 派生属性 # def lay(self): # print('laying') # # # # print(Dog.__bases__) # 查看父类 # # dog = Dog() # 这里Dog没有init方法,会调用父类的init # bird = Bird('ssss') # # bird.lay() # bird.drink() # # bird.eat()
# 新式类默认广度优先,经典类是深度优先 # super 只在python3中存在 # super的本质 :不是单纯找父类 而是根据调用者的节点位置的广度优先顺序来的 class A: def func(self): print('A') class B(A): pass def func(self): super().func() print('B') class C(A): pass def func(self): super().func() print('C') class D(B, C): pass def func(self): super().func() print('D') d = D() d.func() print(B.mro())