类中定义装饰器 装饰类中的实例方法

# def decorate(func):
#     def inner(self,value):
#         func(self,value)
#         print(self.v)
#
#     return inner

class Computer():
    def __init__(self, value):
        self.v = value

    def decorate(func, ):
        def inner(self, value):
            func(self, value)
            print(self.v)

        return inner

    @decorate
    def add(self, value):
        self.v += value
      

    @decorate
    def low(self, value):
        self.v -= value

    @decorate
    def multi(self, value):
        self.v *= value


c = Computer(2)
c.add(3)

上一篇:9.内部类


下一篇:四种内部类inner class,匿名内部类_听韩顺平课笔记