江苏建筑网站建设,广东今天新闻最新消息,ih5专业的h5制作工具,云主机网站的空间在哪里两种继承方式#xff1a;
Class SubClass(FartherClass)#xff1a;子类可以任意调用父类的成员变量、成员函数#xff0c;适合单继承#xff0c;即只继承一个父类。
Super#xff1a;适合多继承
统一用一种#xff0c;不要交叉用。
class FooParent(object): def _…两种继承方式
Class SubClass(FartherClass)子类可以任意调用父类的成员变量、成员函数适合单继承即只继承一个父类。
Super适合多继承
统一用一种不要交叉用。
class FooParent(object): def __init__(self): self.parent I\m the parent. print Parent def bar(self,message): print message, from Parent class FooChild(FooParent): def __init__(self): FooParent.__init__(self) print Child def bar(self,message): FooParent.bar(self,message) print Child bar function. print self.parent if __name____main__: fooChild FooChild() fooChild.bar(HelloWorld)
输出结果
Parent
Child
HelloWorld from Parent
Child bar function.
Im the parent.
Super
class FooParent(object): def __init__(self): self.parent I\m the parent. print Parent def bar(self,message): print message,from Parent class FooChild(FooParent): def __init__(self): super(FooChild,self).__init__() print Child def bar(self,message): 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 function.
Im the parent.