python 中理解super()

super() 函数是用于调用父类(超类)的一个方法。

class singer(people):
super(singer, self).show_my_power(),找singer的父类people,再回到子类singer中。多个继承时,有点像先进后出。下面这个例子,artist实例化后,先找到子类singer(左边),输出"before_super_singer_first",再找到子类actor(右边),输出"before_super_actor_second",然后到达父类people,输出"people"。父类结束后,先到子类actor,输出"after_super_actor_third",再到子类singer,输出"after_super_singer_fourth"。(singer先进actor再进,actor先出singer最后出)

class parent(object):
    def show_my_power(self):
        print("parent")
        
class singer(parent):
    def show_my_power(self):
        print("before_super_singer_first")
        super (singer, self).show_my_power()
        print("after_super_singer_fourth")
        
class actor(parent):
    def show_my_power(self):
        print("before_super_actor_second")
        super (actor, self).show_my_power()
        print("after_super_actor_third")   
        
class artist(singer, actor):
    pass
if __name__ == "__main__":
    a = artist()  #实例化
    a.show_my_power()

输出结果

before_super_singer_first
before_super_actor_second
parent
after_super_actor_third
after_super_singer_fourth

https://blog.csdn.net/Quincuntial/article/details/88663237
http://www.sohu.com/a/331661377_571478

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值