【发布时间】:2017-11-17 10:39:40
【问题描述】:
我希望变量next_id 是只读的。我有一个提示,它应该使用装饰器来实现。有什么帮助吗?
next_id = 1
class Product:
def __init__(self, desc, price, quantity):
global next_id
self.__idNo = next_id
next_id += 1
self.desc = desc
self.price = price
self.quantity = quantity
def get_id(self):
return self.__idNo
def get_total_sum(self):
return self.price * self.quantity
tv_samsung = Product('telewizor kolorowy 40 cali', 2499.00, 5)
print(tv_samsung.get_id())
【问题讨论】:
-
read only是什么意思,那你要怎么更新呢?!
标签: python class oop decorator