def hello(cls):
class wrapper():
def __init__(self, *args, **kwargs):
print("hello, friends!")
self.__wrap = cls(*args, **kwargs)
def __getattr__(self, name: str):
return getattr(self.__wrap, name)
return wrapper
@hello
class ADD():
def __init__(self, a, b) -> None:
self.__a = a
self.__b = b
@property
def sum(self):
return self.__a+self.__b
if __name__ == "__main__":
myadd = ADD(12,14)
print(myadd.sum)
装饰器实现2-类的装饰器
最新推荐文章于 2022-04-19 14:43:14 发布