Python——super

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

上一篇:面对对象(3)(封装,继承,方法的重写)


下一篇:javaSE 笔记 继承 + super + 包 + import + 修饰符 + final + static