通过描述符和类装饰器,自定义staicmethod(静态方法)
通过描述符和类装饰器,自定义staicmethod(静态方法)
class StaticMethod: def __init__(self,func): self.func = func def __get__(self, instance, owner): def deco(*args,**kwargs): return self.func(*args,**kwargs) return deco class Room: @staticmethod def area(width,length,higt): return width * length * higt @StaticMethod def area1(width,length,higt): return width * length * higt print(Room.area(10,20,1)) print(Room.area1(10,20,1)) r = Room() print(r.area(10,20,2)) print(r.area1(10,20,2))