super的用法【自己学习使用 可能有借鉴。。】
super用于在子类中通过不改变父类调用父类方法,例如__init__(),如果父类中有__init__()方法 ,子类中也含有此方法 ,需要调用父类方法时要使用super()方法。
class Animal:
def __init__(self):
self.name = 'animal'
self.role = 'parent'
print('I am your father')
class Dog(Animal):
def __init__(self):
super().__init__()
self.name = 'dog'
print('I am dog')
xiaogou = Dog()
print(xiaogou.name)
print(xiaogou.role)
##
#dog
#parent
这里super().init()的含义就是返回了继承顺序中的上一级,即Animal.__init()