1 ''' 2 私有属性在类外无法通过对象.属性名获取,因此提供setter&getter方法来访问、修改 3 ⼀般定义函数名 get_xx ⽤来获取私有属性,定义 set_xx ⽤来修改私有属性值 4 ''' 5 6 class Master(object): 7 def __init__(self): 8 self.kongfu = '[古法煎饼果子配方]' 9 10 def make_cake(self): 11 print(f'运用{self.kongfu}制作煎饼果子') 12 13 14 class School(object): 15 def __init__(self): 16 self.kongfu = '[黑马煎饼果子配方]' 17 18 def make_cake(self): 19 print(f'运用{self.kongfu}制作煎饼果子') 20 21 22 class Prentice(School, Master): 23 def __init__(self): 24 self.kongfu = '[独创煎饼果子技术]' 25 # 定义私有属性 26 self.__money = 2000000 27 28 # 定义函数:获取私有属性值 get_xx 29 def get_money(self): 30 return self.__money 31 32 # 定义函数:修改私有属性值 set_xx 33 def set_money(self): 34 self.__money = 500 35 36 # 定义私有方法 37 def __info_print(self): 38 print('这是私有方法') 39 40 def make_cake(self): 41 self.__init__() 42 print(f'运用{self.kongfu}制作煎饼果子') 43 44 def make_master_cake(self): 45 Master.__init__(self) 46 Master.make_cake(self) 47 48 def make_school_cake(self): 49 School.__init__(self) 50 School.make_cake(self) 51 52 53 class Tusun(Prentice): 54 pass 55 56 57 xiao_a = Tusun() 58 59 print(xiao_a.get_money()) # 2000000 60 61 xiao_a.set_money() 62 63 print(xiao_a.get_money()) # 500