python 零碎知识 super用法

发布时间:2026/6/23 17:21:30
python 零碎知识 super用法 super是继承用的主要参考1Python super() 函数 | 菜鸟教程 (runoob.com)主要参考2python类中super()_python superaa_wanghua609的博客-CSDN博客class FooParent(object): def __init__(self): self.parent I\m the parent. print (Parent) def bar(self,message): print (%s from Parent % message) class FooChild(FooParent): // 初始化 FooChild def __init__(self): // Note1: 执行父类(FooParent)初始化函数 并继承父类的一切成员变量及函数除私有 // Note2: 继承的结果都给到自己(FooChild)的self里 // Note3: 因此 此时self.parent I\m the parent. // Note4: 并且也可以执行父类(FooParent)里的bar函数 super(FooChild,self).__init__() print (Child) // 其实不算重写写一个与父类bar函数同名的另一个函数 def bar(self,message): super(FooChild, self).bar(message) // 由于已继承 因此可以调用父类的bar函数 print (Child bar fuction) print (self.parent) // 由于已继承 因此可以调用父类的成员变量除私有 if __name__ __main__: fooChild FooChild() fooChild.bar(HelloWorld) 输出 Parent Child HelloWorld from Parent Child bar fuction Im the parent.