1 class Test(object):
2 def __init__(self):
3 self.__num=100
4
5 def getNum (self):#调用私有方法
6 return self.__num
7 def setNum(this,num):
8 if num < 100:
9 this.__num=num
10
num=property(getNum,setNum)
11 t = Test()
12 t.num=20
13 print(t.num)
property进一步用法
1 class Test(object):
2 def __init__(self):
3 self.__num=100#私有属性
4
5
@property
6 def num (self):#调用私有属性
7 return self.__num
8
@num.setter
9 def num(this,num):
10 if num < 100:
11 this.__num=num
12 t = Test()
13 t.num=20
14 print(t.num)