8、python设计模式之装饰模式

来源:互联网 发布:二维码生成打印软件 编辑:程序博客网 时间:2024/05/21 03:59
# 定义一个装饰器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@decoclass 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    """

原创粉丝点击