Second, the second method hides the assignment and value of attributes
This looks like it’s assigning and fetching values to attributes, but it’s actually called the Age method, and when assigning values, it can also judge whether the data meets the requirements, and assign values if it meets the requirements, otherwise the assignment will fail.
classPerson2():def__init__(self,Age):
self.__Age = Age
@property# 方法名为受限制的变量去掉双下划线defAge(self):return self.__Age
# 方法名.setter
@Age.setter
defAge(self,Age):if Age >0:
self.__Age = Age
Person2Obj = Person2(18)
Person2Obj.Age =20print(Person2Obj.Age)