class Student(object):
@property def birth(self): return self.__birth @birth.setter def birth(self, value): self.__birth = value @property def age(self): return 2015 - self.birth
变量加上 _是保护变量,__是私有变量,用_的话
@property def age(self): return 2015 - self._birth
@property def age(self): return 2015 - self.birth
效果一样,不过一个直接调用属性,一个是调用属性函数,容易混淆。
Sign in to make a reply
助人为乐的免爆面包
class Student(object):
变量加上 _是保护变量,__是私有变量,用_的话
@property def age(self): return 2015 - self._birth
@property def age(self): return 2015 - self.birth
效果一样,不过一个直接调用属性,一个是调用属性函数,容易混淆。