摘要:
下文讲述Python代码中super()函数的功能说明,如下所示:
super函数简介
super()函数是Python中的一个内置函数,
用来处理多重继承问题中直接用类名调用父类方法
在使用单继承的时候没问题,
当使用多继承,会涉及到查找顺序(MRO)重复调用等问题
super()函数功能:
用于调用父类中的方法
super函数语法:
super(type[, object-or-type])
-------参数说明------
type:类
object-or-type:类,常为self
-------返回值------
无
-------注意事项-----
Python 3:可以直接使用 super().xxx替换super(Class, self).xxx
例:
Python super函数示例分享
Python3.X
class A:
def add(self, x):
y = x+100
print(y)
class B(A):
def add(self, x):
super().add(x)
b = B()
b.add(8) # 108
Python2.x 实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
class A(object): # Python2.x 记得继承 object
def add(self, x):
y = x+100
print(y)
class B(A):
def add(self, x):
super(B, self).add(x)
b = B()
b.add(8) # 108