class Rectangle: def __init__(self, length, width): self.length = length self.width = width def area(self): areas = self.length * self.width return areas @property # 就像访问属性一样 def area(self): return self.width * self.length @staticmethod # 静态方法 和class类断开联系 def func(): # self 在调用的时候会报错 print(‘staticmethod func’) @classmethod # 类方法 def show(cls): # cls 代表类本身 print(cls) print('show fun')
类装饰器
class Test_Class: def __init__(self, func): self.func = func def __call__(self): print('类') return self.func @Test_Class def fun_test(): print('这是个测试函数')
需要定义 __call__ 方法