super() 函数

用于调用父类(超类)的一个方法

在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,此时可以用 super() 函数实现

实例:

class A:
    def add(self, x):
        y = x + 2
        print(y)
class B(A):
    def add(self, x):
        super().add(x)
b = B()
b.add(2)  # 4
posted @ 2020-05-17 14:53  -费费  阅读(212)  评论(0编辑  收藏  举报