1.设置私有权限的方法:在属性名和方法名 前面 加上两个下划线_
class Master():
def __init__(self):
self.kongfu = "[五香煎饼果子配方]"
self.__money = 2000
def make_cake(self):
print(f"运用{self.kongfu}")
print(f"花费{self.__money}")
def __info_print(self):
print(self.kongfu)
print(self.__money)
class __School():
def __init__(self):
self.kongfu = "[香辣煎饼果子配方]"
def make_cake(self):
print(f"运用{self.kongfu}")
class Prentice(Master):
def __init__(self):
self.kongfu = "[独创煎饼果子配方]"
def make_cake(self):
self.__init__()
print(f"运用{self.kongfu}")
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
class Tusun(Prentice):
def make_old_cake(self):
super().__init__()
super().make_cake()
if __name__ == '__main__':
result = Tusun()
r = Prentice()
s = __School()
r.make_master_cake()
print(r.kongfu)
print(s.kongfu)
二、获取和修改私有属性和方法
class Master():
def __init__(self):
self.kongfu = "[五香煎饼果子配方]"
def make_cake(self):
print(f"运用{self.kongfu}")
print(f"花费{self.__money}")
class School():
def __init__(self):
self.kongfu = "[香辣煎饼果子配方]"
def make_cake(self):
print(f"运用{self.kongfu}")
class Prentice(Master,School):
def __init__(self):
self.kongfu = "[独创煎饼果子配方]"
self.__money = 500
def get_money(self):
return self.__money
def set_money(self,m):
self.__money = m
def __info_print(self):
print(self.kongfu)
print(self.__money)
def make_cake(self):
self.__init__()
print(f"运用{self.kongfu}制作煎饼果子")
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake(self)
class Tusun(Prentice):
pass
if __name__ == '__main__':
s = Prentice()
result = Tusun()
print(s.get_money())
s.set_money(200)
print(s.get_money())