class Parent(object): def __init__(self): self.parent = 'I am the parent' print('parent') def bar(self, message): print(message, 'from parent') class FooChile(Parent): def __init__(self): super(FooChile, self).__init__() print('Child') def bar(self, message): super(FooChile, self).bar(message) print('Child bar function') print(self.parent) if __name__ == '__main__': fooChild = FooChile() fooChild.bar('Hello world')
super()在类继承中的使用:
1、在子类的 __init__(*args, **kwargs) (此处是自已用的初始化参数)方法中使用,super(子类名, self).__init__(*args, **kwargs),此处的参数是传给父类的
2、在子类的方法中使用 super().func() 重写父类的func()方法,但自身的func()方法与可以执行。