商品案例 用法

 1 class Goods:
 2     def __init__(self,price,discount):
 3         self.price = price
 4         self.discount = discount
 5 
 6     @property
 7     def price1(self):
 8         return self.price * self.discount
 9 
10     @price1.setter   # 可以设置实例属性
11     def price1(self, val):
12         self.discount = val
13 
14     @price1.deleter  # 可以删除实例属性
15     def price1(self):
16         del self.price
17 
18 
19 g1 = Goods(100,0.8)
20 print(g1.price1)
21 g1.price1 = 0.45
22 print(g1.price1)
23 输出:
24 80.0
25 45.0

 

posted @ 2020-03-16 15:40  竹石2020  阅读(136)  评论(0)    收藏  举报