Python類繼承及super()函數(shù)使用說(shuō)明
Python中單類繼承
Python是一門面向?qū)ο蟮木幊陶Z(yǔ)言,支持類繼承。
新的類稱為子類(Subclass),被繼承的類稱為父類、基類或者超類。子類繼承父類后,就擁有父類的所有特性。
類繼承的簡(jiǎn)單例子:
普通類方法繼承
class Fruit():
? ? def color(self):
? ? ? ? print("colorful")
class Apple(Fruit):
? ? pass
class Orange(Fruit):
? ? pass
apple = Apple()
orange = Orange()
apple.color()
orange.color()
# 輸出
# colorful
# colorful這里Fruit為父類,Apple和Orange為子類,子類繼承了父類的特性,因此Apple和Orange也擁有Color方法。
子類除了可以繼承父類的方法,還可以覆蓋父類的方法:
class Fruit():
? ? def color(self):
? ? ? ? print("colorful")
class Apple(Fruit):
? ? def color(self):
? ? ? ? print("red")
class Orange(Fruit):
? ? def color(self):
? ? ? ? print("orange")
apple = Apple()
orange = Orange()
apple.color()
orange.color()
# 輸出
# red
# orange子類可以在繼承父類方法的同時(shí),對(duì)方法進(jìn)行重構(gòu)。
這樣一來(lái),子類的方法既包含父類方法的特性,同時(shí)也包含子類自己的特性:
class Fruit():
? ? def color(self):
? ? ? ? print("Fruits are colorful")
class Apple(Fruit):
? ? def color(self):
? ? ? ? super().color()
? ? ? ? print("Apple is red")
class Orange(Fruit):
? ? def color(self):
? ? ? ? super().color()
? ? ? ? print("Orange is orange")
apple = Apple()
orange = Orange()
apple.color()
orange.color()
# 輸出
# Fruits are colorful
# Apple is red
# Fruits are colorful
# Orange is orange初始化函數(shù)繼承
如果我們需要給類傳入?yún)?shù),需要使用初始化函數(shù)。如果所有子類中部分參數(shù)是相同的,那么可以在父類的初始化函數(shù)中定義這些參數(shù),然后子類繼承父類的初始化函數(shù),這樣所有子類就可共享這些參數(shù),而不需要在每個(gè)子類中單獨(dú)定義。
初始化函數(shù)的繼承:
class Fruit():
? ? def __init__(self, color, shape):
? ? ? ? self.color = color
? ? ? ? self.shape = shape
class Apple(Fruit):
? ? def __init__(self, color, shape, taste):
? ? ? ? Fruit.__init__(self, color, shape) # 等價(jià)于super().__init__(color, shape)
? ? ? ? self.taste = taste
? ??
? ? def feature(self):
? ? ? ? print("Apple's color is {}, shape is {} and taste {}".format(
? ? ? ? ? ? self.color, self.shape, self.taste))
class Orange(Fruit):
? ? def __init__(self, color, shape, taste):
? ? ? ? Fruit.__init__(self, color, shape)
? ? ? ? self.taste = taste
? ??
? ? def feature(self):
? ? ? ? print("Orange's color is {}, shape is {} and taste {}".format(
? ? ? ? ? ? self.color, self.shape, self.taste))
apple = Apple("red", "square", "sour")
orange = Orange("orange", "round", "sweet")
apple.feature()
orange.feature()
# 輸出
# Apple's color is red, shape is square and taste sour
# Orange's color is orange, shape is round and taste sweetPython中多類繼承
在單類繼承中,super()函數(shù)用于指向要繼承的父類,且不需要顯式的寫出父類名稱。
但是在多類繼承中,會(huì)涉及到查找順序(MRO)、鉆石繼承等問(wèn)題。
MRO 是類的方法解析順序表, 也就是繼承父類方法時(shí)的順序表。
鉆石繼承
? ? A ? ?/ \ ? B ? C ? ?\ / ? ? D
如圖所示,A是父類,B和C繼承A,D繼承B和C。
下面舉例說(shuō)明鉆石繼承的繼承順序
class Plant():
? ? def __init__(self):
? ? ? ? print("Enter plant")
? ? ? ? print("Leave plant")
class Fruit(Plant):
? ? def __init__(self):
? ? ? ? print("Enter Fruit")
? ? ? ? super().__init__()
? ? ? ? print("Leave Fruit")
class Vegetable(Plant):
? ? def __init__(self):
? ? ? ? print("Enter vegetable")
? ? ? ? super().__init__()
? ? ? ? print("Leave vegetable")
class Tomato(Fruit, Vegetable):
? ? def __init__(self):
? ? ? ? print("Enter Tomato")
? ? ? ? super().__init__()
? ? ? ? print("Leave Tomato")
tomato = Tomato()
print(Tomato.__mro__)
# 輸出
# Enter Tomato
# Enter Fruit
# Enter vegetable
# Enter plant
# Leave plant
# Leave vegetable
# Leave Fruit
# Leave Tomato
# (<class '__main__.Tomato'>, <class '__main__.Fruit'>, <class '__main__.Vegetable'>, <class '__main__.Plant'>, <class 'object'>)以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python的Dataframe取兩列時(shí)間值相差一年的所有行方法
今天小編就為大家分享一篇使用Python的Dataframe取兩列時(shí)間值相差一年的所有行方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
python編程學(xué)習(xí)np.float 被刪除的問(wèn)題解析
這篇文章主要為大家介紹了python編程學(xué)習(xí)np.float 被刪除的問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
使用Python實(shí)現(xiàn)一鍵往Word文檔的表格中填寫數(shù)據(jù)
在工作中,我們經(jīng)常遇到將Excel表中的部分信息填寫到Word文檔的對(duì)應(yīng)表格中,以生成報(bào)告,方便打印,所以本文小編就給大家介紹了如何使用Python實(shí)現(xiàn)一鍵往Word文檔的表格中填寫數(shù)據(jù),文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-12-12
Selenium結(jié)合BeautifulSoup4編寫簡(jiǎn)單的python爬蟲
這篇文章主要介紹了Selenium結(jié)合BeautifulSoup4編寫簡(jiǎn)單的python爬蟲,幫助大家更好的理解和學(xué)習(xí)python 爬蟲的相關(guān)知識(shí),感興趣的朋友可以了解下2020-11-11
python文本數(shù)據(jù)處理學(xué)習(xí)筆記詳解
這篇文章主要為大家詳細(xì)介紹了python文本數(shù)據(jù)處理學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
Python表格處理模塊xlrd在Anaconda中的安裝方法
本文介紹在Anaconda環(huán)境下,安裝Python讀取.xls格式表格文件的庫(kù)xlrd的方法,xlrd是一個(gè)用于讀取Excel文件的Python庫(kù),本文介紹了xlrd庫(kù)的一些主要特點(diǎn)和功能,感興趣的朋友一起看看吧2024-04-04
Pycharm關(guān)于遠(yuǎn)程JupyterLab以及JupyterHub登錄問(wèn)題
這篇文章主要介紹了Pycharm關(guān)于遠(yuǎn)程JupyterLab以及JupyterHub登錄問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

