python @property
class Person(object): def __init__(self, age): self.age = age @property def age(self): print('i am the get age of Person ...') return self._age @age.setter def age(self, value): print('i am the set age of Person ...') if value > 18: raise ValueError('should under 18') self._age = value class Student(Person): @property def age(self): print('i am the get age of Student ...') # 在子类中调用父类的方法 return super().age @age.setter def age(self, value): print('i am the set age of Student ...') # print(Student.__mro__) # 在子类中调用父类的方法 super(Student, Student).age.__set__(self, value) p = Person(18) print(p.age) s = Student(12) print(s.age) # property定义只读属性 class A(object): def __init__(self): super().__init__() @property def name(self): return 'aaa' a = A() # 会报错 a.name = 'fffff' print(a.name)