python 属性property的两种实现
方法一
class Money(object):
def __init__(self):
self.__money = 0
def get_money(self):
return self.__money
def set_money(self, value):
if isinstance(value, int):
self.__money = value
else:
print("error: 不是整型数字")
money = property(get_money, set_money)
a = Money()
a.money = 100
print(a.money)
方法二
class Money(object):
def __init__(self):
self.__money = 0
@property
def money(self):
return self.__money
@money.setter
def money(self, value):
if isinstance(value, int):
self.__money = value
else:
print("error: 不是整型数字")
a = Money()
a.money = 100
print(a.money)