Python教程

Python——super

本文主要是介绍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

这篇关于Python——super的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!