python3 property呼叫私有方法用法
阿新 • • 發佈:2019-01-23
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)
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)