#1 全局函数装饰类方法 def a_(fun): ****** class A(object): def __init__(self): self.a = 100 @a_#使用全局函数装饰 def func(self, num): ****** c = A() c_ = c.func(100) print(c_) # 200 def a_(fun): def inner(self,num): c =fun(self,num) return c return inner class A(object): def __init__(self): self.a = 100 @a_#使用全局函数装饰 def func(self,num): return self.a+num c = A() c_ = c.func(100) print(c_) #2 类方法装饰实例方法 class A(object): def __init__(self): self.a = 100 def b_(fun): ****** @b_#使用类内方法装饰 def func1(self, num): ****** c_1 = c .func1(102) print(c_1) # 202 class A(object): def __init__(self): self.a = 100 def b_(fun): def inner(self,num): print('正在调用%s'%fun.__name__) a = fun(self, num) print(a) print('调用结束%s'%fun.__name__) return a return inner @b_#使用类内方法装饰 def func1(self,num): return self.a +num c_1 = c .func1(102) print(c_1)# 202 #3 类装饰函数方法 class B(object): def __init__(self): ****** def __***__() ****** @B#使用类来装饰 def cc(num,num1): return num+num1 d = cc (11,22) # 33 print(d) class B(object): def __init__(self, func): self.func = func def __call__(self, *args, **kwargs): c = self.func(*args,**kwargs) return c @B#使用类来装饰 def cc(num,num1): return num+num1 d = cc (11,22) print(d) #使用类装饰类 import time class A(object): def __init__(self,func): self.a = 100 self.func = func def __call__(self, *args, **kwargs): print(time.time()) c = self.func(self,*args,**kwargs) print(c,'这是c') print(time.time()) return c class B: def __init__(self): self.a = 200 @A def func(self,num): return num+self.a b = B() c = b.func(100) print(c)#猜想结果是多少200