类方法装饰类方法
把一个方法变成类中的方法
之后调用此方法不需要对类实例化后在调用
直接通过类.方法即可调用
1 class Goods: 2 __discount = 0.5 3 def __init__(self,name,price): 4 self.name = name 5 self.__price = price # 我才不会让别人知道我的价格 6 @property 7 def price(self): 8 return self.__price * Goods.__discount 9 @classmethod # 类方法,把一个方法变成类中的方法, 10 # 这个方法直接可以被类调用,不在需要对象调用了 11 def chang_dis(cls,new_discount): 12 Goods.__discount = new_discount 13 suyang = Goods("苏阳",0.5) 14 print(suyang.price) # 0.25 15 Goods.chang_dis(0.3) # 原来的方式改还需要输入实例化对象 16 print(suyang.price) # 0.15