python面向对象

下面的代码展示了累的继承,我们可以手动调用父类的方法,当我们调用子类的一个方法的时候,如果子类没有实现,而父类实现类,那么就会直接调用父类的方法,如果父类没有实现的方法,但是子类实现类,父类可以调用,子类调用以后直接使用的是子类的实现方法,代码如下:

class A(object):
    def go(self):
        print ("go A go ")
    def stop(self):
        print ("stop A stop")
    def pause(self):
        return 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(C,B):
    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):
        super(D,self).pause()
        print("wait D wait")
class E(B,C):
    pass
a=A()
b=B()
c=C()
d=D()
e=E()
a.go()
b.go()
c.go()
d.go()
e.go()
print("134")
a.stop()
print("1")
b.stop()
print("2")
c.stop()
print("3")
d.stop()
print("4")
e.stop()
print("1234")
a.pause()
b.pause()
c.pause()
d.pause()
e.pause()

运行结果
"""
go A go 
go B go !
go A go 
go C go !
go B go !
go D go
go B go !
134
stop A stop
1
stop A stop
2
stop A stop
stop C stop!
3
stop A stop
stop C stop!
stop D stop
4
stop A stop
stop C stop!
1234
wait D wait
 
"""




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值