继承和super函数的用法【也是新式类和旧式类的区别之处】

# coding=utf-8
class A(object):
    def go(self):
        print("go A go!")

    def stop(self):
        print("stop A stop!")

    def pause(self):
        raise Exception("Not Implemented")


class B(A):
    def go(self):
        super(B, self).go( )
        print("go B go!")


class C(A):
    def go(self):
        super(C, self).go( )
        print("go C go!")

    def stop(self):
        super(C, self).stop( )
        print("stop C stop!")


class D(B, C):
    def go(self):
        super(D, self).go( )
        print("go D go!")

    def stop(self):
        super(D, self).stop( )
        print("stop D stop!")

    def pause(self):
        try:
            print("wait D wait!")
        except:
            raise


class E(B, C): pass


a = A( )
b = B( )
c = C( )
d = D( )
e = E( )
# a.go( )  # go A go!
# b.go( )  # go A go! go B go!-----------------------------------------继承A
# c.go( )  # go A go! go C go!-----------------------------------------继承A
# d.go( )  # go A go! go C go! go B go! go D go!-----------------------继承B,C
# e.go( )  # go A go! go C go! go B go!--------------------------------继承B,C

# a.stop( )  # stop A stop!
# b.stop( )  # stop A stop!--------------------------------------------继承A
# c.stop( )  # stop A stop! stop C stop!-------------------------------继承A
# d.stop( )  # stop A stop! stop C stop! stop D stop!------------------继承B,C
# e.stop( )  # stop A stop! stop C stop!-------------------------------继承B,C

# a.pause( )  # ... Exception: Not Implemented
# b.pause( )  # ... Exception: Not Implemented
# c.pause( )  # ... Exception: Not Implemented
# d.pause( )  # wait D wait!  ... Exception: Not Implemented(把错误抛给父类)
# e.pause( )  # ... Exception: Not Implemented

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值