Python中子类怎样调用父类方法

Python029

Python中子类怎样调用父类方法,第1张

子类调用父类函数有以下方法

直接写类名调用

用 super(type, obj).method(arg)方法调用。

在类定义中调用本类的父类方法,可以直接用super().method(arg)

class A:

def method(self, arg):

pass

class B(A):

def method(self, arg):

#A.method(self,arg)# 1

#super(B, self).method(arg)# 2

super().method(arg) # 3

子类调用父类函数有以下方法:

直接写类名调用

用 super(type, obj).method(arg)方法调用。

在类定义中调用本类的父类方法,可以直接用super().method(arg)

1

2

3

4

5

6

7

8

9

class A:

def method(self, arg):

pass

class B(A):

def method(self, arg):

#A.method(self,arg)# 1

#super(B, self).method(arg)# 2

super().method(arg) # 3