# 定义一个装饰器
def deco(cls):
class NewClass:
def __init__(self, age, color):
self.wrapped = cls(age)
self.color = color
def display(self):
print(self.wrapped.age)
print(self.color)
return NewClass
@deco
class Cat:
def __init__(self, age):
self.age = age
def display(self):
print(self.age)
# 测试
if __name__ == '__main__':
cat = Cat(6, 'white')
cat.display()
"""
6
white
"""
8、python设计模式之装饰模式
最新推荐文章于 2024-07-28 15:46:11 发布