from datetime import date, datetime class User: def __init__(self, name, birthday): self.name = name self.birthday = birthday self._age = 0 # def get_age(self): # return datetime.now().year - self.birthday.year @property #动态属性 def age(self): #属性描述符,get方法 return datetime.now().year - self.birthday.year @age.setter def age(self, value): self._age = value if __name__ == "__main__": user = User("bobby", date(year=1987, month=1, day=1)) user.age = 30 print (user._age) print(user.age)
原文地址:https://www.cnblogs.com/Erick-L/p/8876276.html
时间: 2024-10-09 11:43:04