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