super函数

super(type, obj) #返回绑定超类的实例(要求obj必须为type类型的实例)

super() #返回绑定的超类的实例,等同于(class,实例方法的第一个参数),此方法需要用在方法内部。

作用:

返回绑定超类的实例,用超类的实例来调用其自身方法

class A(object):
    def hello(self):
        print("A类的hello(self)")


class B(A):
    def hello(self):
        print("B类的Hello(self)")

    def super_hello(self):#此方法调用基类的hello方法

        super().hello()#super(B,self).hello() 打印结果:A类的hello(self)


b=B()
b.hello()  #B类的Hello(self)

#用 super函数调用基类的方法:
super(B,b).hello()#A类的hello(self)
B.__base__.hello(b)#作用等同于上一条语句

b.super_hello()#A类的hello(self)




你可能感兴趣的:(python学习)