class FooParent(object): def __init__(self): self.parent = 'I am the parent' print('Parent') def bar(self, message): print("%s from Parent" % message) class FooChild(FooParent): def __init__(self): # 找到FooChild父类,即FooParent,并执行FooParent的初始化函数 super(FooChild, self).__init__() print('Child') def bar(self, message): # 找到FooChild父类,即FooParent,并执行FooParent的bar函数 super(FooChild, self).bar(message) print('Child bar fuction') print(self.parent) if __name__ == '__main__': fooChild = FooChild() fooChild.bar('HelloWorld')
运行结果:
Parent
Child
HelloWorld from Parent
Child bar fuction
I am the parent