全球快資訊:Python之?向?qū)ο?繼承二

2022-12-20 15:02:42 來源:51CTO博客


(相關(guān)資料圖)

六、多層繼承

故事:N年后,daqiu?了,想要把所有技術(shù)傳承給??的徒弟。class Master(object): def __init__(self):   self.kongfu = "[古法煎餅果?配?]" def make_cake(self):   print(f"運?{self.kongfu}制作煎餅果?")class School(object): def __init__(self):   self.kongfu = "[??煎餅果?配?]" def make_cake(self):   print(f"運?{self.kongfu}制作煎餅果?")class Prentice(School, Master): def __init__(self):   self.kongfu = "[獨創(chuàng)煎餅果?配?]" 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): passxiaoqiu = Tusun()xiaoqiu.make_cake()xiaoqiu.make_school_cake()xiaoqiu.make_master_cake()

七、super()調(diào)??類?法

class Master(object): def __init__(self):   self.kongfu = "[古法煎餅果?配?]" def make_cake(self):   print(f"運?{self.kongfu}制作煎餅果?")   class School(Master): def __init__(self):   self.kongfu = "[??煎餅果?配?]" def make_cake(self):   print(f"運?{self.kongfu}制作煎餅果?")   # ?法2.1   # super(School, self).__init__()   # super(School, self).make_cake()   # ?法2.2   super().__init__()   super().make_cake()   class Prentice(School): def __init__(self):   self.kongfu = "[獨創(chuàng)煎餅果?技術(shù)]" def make_cake(self):   self.__init__()   print(f"運?{self.kongfu}制作煎餅果?") # ?類調(diào)??類的同名?法和屬性:把?類的同名屬性和?法再次封裝 def make_master_cake(self):   Master.__init__(self)   Master.make_cake(self) def make_school_cake(self):   School.__init__(self)   School.make_cake(self)    # ?次性調(diào)??類的同名屬性和?法 def make_old_cake(self):   # ?法?:代碼冗余;?類類名如果變化,這?代碼需要頻繁修改   # Master.__init__(self)   # Master.make_cake(self)   # School.__init__(self)   # School.make_cake(self)   # ?法?: super()   # ?法2.1 super(當(dāng)前類名, self).函數(shù)()   # super(Prentice, self).__init__()   # super(Prentice, self).make_cake()   # ?法2.2 super().函數(shù)()   super().__init__()   super().make_cake()daqiu = Prentice()daqiu.make_old_cake()注意:使?super() 可以?動查找?類。調(diào)?順序遵循 __mro__ 類屬性的順序。?較適合單繼承使?。

八、私有權(quán)限

8.1 定義私有屬性和?法

在Python中,可以為實例屬性和?法設(shè)置私有權(quán)限,即設(shè)置某個實例屬性或?qū)嵗?法不繼承給?類。故事:daqiu把技術(shù)傳承給徒弟的同時,不想把??的錢(2000000個億)繼承給徒弟,這個時候就要為 錢 這個實例屬性設(shè)置私有權(quán)限。設(shè)置私有權(quán)限的?法:在屬性名和?法名 前? 加上兩個下劃線 __。class Master(object): def __init__(self):   self.kongfu = "[古法煎餅果?配?]" def make_cake(self):   print(f"運?{self.kongfu}制作煎餅果?")class School(object): def __init__(self):   self.kongfu = "[??煎餅果?配?]" def make_cake(self):   print(f"運?{self.kongfu}制作煎餅果?")class Prentice(School, Master): def __init__(self):   self.kongfu = "[獨創(chuàng)煎餅果?配?]"   # 定義私有屬性   self.__money = 2000000 # 定義私有?法 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 daqiu = Prentice()# 對象不能訪問私有屬性和私有?法# print(daqiu.__money)# daqiu.__info_print()xiaoqiu = Tusun()# ?類?法繼承?類的私有屬性和私有?法# print(xiaoqiu.__money) # ?法訪問實例屬性__money# xiaoqiu.__info_print()注意:私有屬性和私有?法只能在類??訪問和修改

8.2 獲取和修改私有屬性值

在Python中,?般定義函數(shù)名 get_xx ?來獲取私有屬性,定義 set_xx ?來修改私有屬性值。class Master(object): def __init__(self):   self.kongfu = "[古法煎餅果?配?]" def make_cake(self):   print(f"運?{self.kongfu}制作煎餅果?")   class School(object): def __init__(self):   self.kongfu = "[??煎餅果?配?]" def make_cake(self):   print(f"運?{self.kongfu}制作煎餅果?")   class Prentice(School, Master): def __init__(self):   self.kongfu = "[獨創(chuàng)煎餅果?配?]"   self.__money = 2000000   # 獲取私有屬性 def get_money(self):   return self.__money # 修改私有屬性 def set_money(self):   self.__money = 500 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): passdaqiu = Prentice()xiaoqiu = Tusun()# 調(diào)?get_money函數(shù)獲取私有屬性money的值print(xiaoqiu.get_money())# 調(diào)?set_money函數(shù)修改私有屬性money的值xiaoqiu.set_money()print(xiaoqiu.get_money())

標(biāo)簽: 屬性設(shè)置 這個時候

上一篇:Python之?向?qū)ο笃渌?/a>
下一篇:環(huán)球快訊:如何使用 Spring Boot 構(gòu)建一個簡單的 Web 應(yīng)用程序