Python中属性
属性定义的两种方式:
1、num1=property(GetNum,SetNum)
class Pro(): def __init__(self): self._num=0 def GetNum(self): return self._num def SetNum(self,num): self._num=num num1=property(GetNum,SetNum) c=Pro() print(c.GetNum()) c.SetNum(10) print(c.GetNum()) print(c.num1)
2、通过装饰器的形式
class Pro(): def __init__(self): self._num=0 @property def num(self): return self._num @num.setter def num(self,num): self._num=num c=Pro() print(c.num) c.num=2 print(c.num)