Python内置函数之classmetho staticmethod

当对类的静态属性进行修改时(不需要借助于对象就行类静态属性的修改)

class Goods:
    discount = 0.5
    def __init__(self,name,price):
        self.name = name
        self.__price = price

    @property
    def price(self):
        return  self.__price * Goods.discount
    @classmethod 
    def changeDiscount(cls,new_discount):
        cls.discount = new_discount

apple = Goods('apple',10)
Goods.changeDiscount(0.1)
print(apple.price)

猜你喜欢

转载自www.cnblogs.com/wc89/p/10390447.html