python子类怎么调用父类方法
python中类的初始化方法是__init__(),因此父类子类的初始化方法都是这个,如果子类不实现这个函数,初始化时调用父类的初始化函数,如果子类实现这个函数,就覆盖了父类的这个函数,既然继承父类,就要在这个函数里显式调用一下父类的__init__(),这跟C++,jAVA不一样,他们是自动调用父类初始化函数的。
调用父类函数有以下方法:
classA:
defmethod(self,arg):
pass
classB(A):
defmethod(self,arg):
#A.method(self,arg)#1
#super(B,self).method(arg)#2
su
1.直接写类名调用
2.用super(type,obj).method(arg)方法调用。
3.在类定义中调用本类的父类方法,可以直接super().method(arg).
在代码中调用对象的父类的方法的示例:
ob=B()
super(B,ob).method(arg)#调用classB的父类classA的method。
初始化中调用父类初始化方法示例:
classB(A):
def__init__(self):
super().__init__()
以上内容为大家介绍了python培训之子类怎么调用父类方法,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






