class Super:
def method(self):
print('super method')
def delegate(self):
self.action()
class Inheritor(Super): #1 获得父类的一切内容
pass
class Replacer(Super): #2 覆盖父类的method
def method(self):
print('replacer.method')
class Extender(Super): #3 覆盖并回调method 定制父类的method方法
def method(self):
print('extender.method')
Super.method(self)
print('ending extend.method')
class Provider(Super): #4
def action(self): # 实现super的delegate方法预期的action方法
print('provider.action')
if __name__ == '__main__':
for i in (Inheritor,Replacer,Extender):
print(i.__name__)
i().method()
x = Provider()
x.delegate()
'''
Inheritor
super method
provider.action
Replacer
replacer.method
provider.action
Extender
extender.method
super method
ending extend.method
provider.action
'''
python类接口,了解一下
最新推荐文章于 2025-09-17 08:00:00 发布
本文通过Python示例代码展示了类继承的基本概念,包括继承父类、覆盖方法、扩展父类功能及委托方法等。通过具体实例帮助理解面向对象编程中继承机制的工作原理。
1491

被折叠的 条评论
为什么被折叠?



