# 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
继承和super函数的用法【也是新式类和旧式类的区别之处】
最新推荐文章于 2021-05-20 03:29:01 发布