1 class Person(): 2 def __init__(self, name, age): 3 # 公有属性 4 self.name = name 5 # 私有属性 6 self.__age = age 7 8 # get方法 : 取值 有返回值 9 def getAge(self): 10 return self.__age 11 # set方法 : 赋值 函数有形参 12 def setAge(self, age): 13 if age < 0: 14 self.__age = 0 15 else: 16 self.__age = age 17 def getName(self): 18 return self.name 19 def setName(self, name): 20 if name == "123": 21 name = "lalalala" 22 self.name = name 23 24 25 # 26 per1 = Person("曾黎", 12) 27 print(per1.name) 28 print(per1.getAge()) 29 per1.setAge(-20) 30 print(per1.getAge()) 31 32 print(per1.getName()) 33 per1.setName("123") 34 print(per1.name)