python property的用法
用法一:
class Test(object):
def __init__(self):
# 私有化
self.__num = 100 #名字重整_Test__num
def setNum(self,newNum):
print("-----setter")
self.__num = newNum
def getNum(self):
print("-----getter")
return self.__num
num = property(getNum,setNum)
t = Test();
# t.__num = 200;
# print(t.__num)
print(t.getNum())
t.setNum(50)
print(t.getNum())
print('-------------------------')
t.num = 200 #相当于调用了 t.setNum(200)
print(t.num) # 相当于调用了 t.getNum()
# 注意点
# t.num 到底是调用了getNum()还是SetNum(),要根据实际的场景来判断,
# 1.如果是给t.num赋值,那么一定调用setNum()
# 2.如果是获取t.num的值,那么就是调用getNum()
# property的作用:相当于把方法进行了封装,开发者在对属性设置数据的时候更方便
用法二:
class Test(object):
def __init__(self):
# 私有化
self.__num = 100 #名字重整_Test__num
@property
def num(self):
print("-----getter")
return self.__num
@num.setter
def num(self,newNum):
print("-----setter")
self.__num = newNum
t = Test();
t.num = 200 #相当于调用了 t.setNum(200)
print('--------------------------')
print(t.num) # 相当于调用了 t.getNum()