一、使用方法,封装变量.
1 # 使用方法,封装变量. 2 class Wife: 3 def __init__(self, name, age, weight): 4 self.name = name 5 # 本质:障眼法(实际将变量名改为:_类名__age) 6 # self.__age = age 7 self.set_age(age) 8 # self.__weight = weight 9 self.set_weight(weight) 10 11 # 提供公开的读写方法 12 def get_age(self): 13 return self.__age 14 15 def set_age(self, value): 16 if 21 <= value <= 31: 17 self.__age = value 18 else: 19 raise ValueError("我不要") 20 21 # 提供公开的读写方法 22 def get_weight(self): 23 return self.__weight 24 25 def set_weight(self, value): 26 if 40 <= value <= 60: 27 self.__weight = value 28 else: 29 raise ValueError("我不要") 30 31 """ 32 w01 = Wife("铁锤公主", 87, 87) 33 # 重新创建了新实例变量(没有改变类中定义的__age) 34 # w01.__age = 107 35 w01._Wife__age = 107 # (修改了类中定义的私有变量) 36 37 print(w01.__dict__)# python内置变量,存储对象的实例变量. 38 """ 39 40 w01 = Wife("铁锤公主", 30, 50) 41 w01.set_age(25) 42 w01.set_weight(55) 43 print(w01.get_age()) 44 print(w01.get_weight())
二、使用property(读取方法,写入方法)对象,封装变量.
1 """ 2 使用property(读取方法,写入方法)对象,封装变量. 3 """ 4 class Wife: 5 def __init__(self, name, age, weight): 6 self.name = name 7 # self.set_age(age) 8 self.age = age 9 # self.set_weight(weight) 10 self.weight = weight 11 12 def get_age(self): 13 return self.__age 14 15 def set_age(self, value): 16 if 21 <= value <= 31: 17 self.__age = value 18 else: 19 raise ValueError("我不要") 20 21 # 属性 property对象拦截对age类变量的读写操作 22 age = property(get_age,set_age) 23 24 def get_weight(self): 25 return self.__weight 26 27 def set_weight(self, value): 28 if 40 <= value <= 60: 29 self.__weight = value 30 else: 31 raise ValueError("我不要") 32 33 weight = property(get_weight,set_weight) 34 35 w01 = Wife("铁锤公主", 30, 50) 36 # w01.set_age(25) 37 w01.age = 25 38 print(w01.age) 39 w01.weight = 45 40 print(w01.weight)
三、使用标准属性,封装变量.
1 """ 2 使用标准属性,封装变量. 3 """ 4 5 class Wife: 6 def __init__(self, name, age, weight): 7 self.name = name 8 self.age = age 9 self.weight = weight 10 11 @property # 创建property对象,只负责拦截读取操作 12 def age(self): 13 return self.__age 14 15 @age.setter # 只负责拦截写入操作 16 def age(self, value): 17 if 21 <= value <= 31: 18 self.__age = value 19 else: 20 raise ValueError("我不要") 21 22 @property 23 def weight(self): 24 return self.__weight 25 26 @weight.setter 27 def weight(self, value): 28 if 40 <= value <= 60: 29 self.__weight = value 30 else: 31 raise ValueError("我不要") 32 33 w01 = Wife("铁锤公主", 30, 50) 34 w01.age = 25 35 print(w01.age) 36 w01.weight = 45 37 print(w01.weight)
四、属性内存图
五、封装设计思想(分而治之、变则疏之、高内聚、低耦合)
1 """ 2 封装设计思想 3 需求:老张开车去东北 4 """ 5 6 7 class Person: 8 def __init__(self, name): 9 self.name = name 10 11 @property 12 def name(self): 13 return self.__name 14 15 @name.setter 16 def name(self, value): 17 self.__name = value 18 19 def go_to(self, str_postion, type): 20 """ 21 去 22 :param str_postion: 位置 23 :param type: 方式 24 """ 25 print(self.name, "去", str_postion) 26 type.run(str_postion) 27 28 29 class Car: 30 def run(self, str_position): 31 """ 32 行驶 33 :param str_position: 位置 34 """ 35 print("汽车行驶到:", str_position) 36 37 38 lz = Person("老张") 39 car = Car() 40 # 老张开车去东北 41 lz.go_to("东北", car)
来源:https://www.cnblogs.com/libotao/p/12496955.html