Property用法

class Goods:
    #设置默认价格和折扣
    def __init__(self):
        # 原价
        self.original_price = 100
        # 折扣
        self.discount = 0.8

    @property
    def price(self):
        #实际价格 = 原价 * 折扣
        new_price = self.original_price * self.discount
        return new_price

    @price.setter
    #更改原始价格
    def price(self,value):
        self.original_price = value

    @price.deleter
    #删除价格
    def price(self):
        del self.original_price

obj = Goods()
obj.price        #获取商品价格
obj.price = 200  #修改商品原价
print(obj.price)
del obj.price    #删除商品原价

猜你喜欢

转载自www.cnblogs.com/425500828zjy/p/8995804.html
今日推荐