Python類(lèi)繼承及super()函數(shù)使用說(shuō)明
Python中單類(lèi)繼承
Python是一門(mén)面向?qū)ο蟮木幊陶Z(yǔ)言,支持類(lèi)繼承。
新的類(lèi)稱(chēng)為子類(lèi)(Subclass),被繼承的類(lèi)稱(chēng)為父類(lèi)、基類(lèi)或者超類(lèi)。子類(lèi)繼承父類(lèi)后,就擁有父類(lèi)的所有特性。
類(lèi)繼承的簡(jiǎn)單例子:
普通類(lèi)方法繼承
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為父類(lèi),Apple和Orange為子類(lèi),子類(lèi)繼承了父類(lèi)的特性,因此Apple和Orange也擁有Color方法。
子類(lèi)除了可以繼承父類(lèi)的方法,還可以覆蓋父類(lèi)的方法:
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子類(lèi)可以在繼承父類(lèi)方法的同時(shí),對(duì)方法進(jìn)行重構(gòu)。
這樣一來(lái),子類(lèi)的方法既包含父類(lèi)方法的特性,同時(shí)也包含子類(lèi)自己的特性:
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ù)繼承
如果我們需要給類(lèi)傳入?yún)?shù),需要使用初始化函數(shù)。如果所有子類(lèi)中部分參數(shù)是相同的,那么可以在父類(lèi)的初始化函數(shù)中定義這些參數(shù),然后子類(lèi)繼承父類(lèi)的初始化函數(shù),這樣所有子類(lèi)就可共享這些參數(shù),而不需要在每個(gè)子類(lèi)中單獨(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中多類(lèi)繼承
在單類(lèi)繼承中,super()函數(shù)用于指向要繼承的父類(lèi),且不需要顯式的寫(xiě)出父類(lèi)名稱(chēng)。
但是在多類(lèi)繼承中,會(huì)涉及到查找順序(MRO)、鉆石繼承等問(wèn)題。
MRO 是類(lèi)的方法解析順序表, 也就是繼承父類(lèi)方法時(shí)的順序表。
鉆石繼承
? ? A ? ?/ \ ? B ? C ? ?\ / ? ? D
如圖所示,A是父類(lèi),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)文章
django美化后臺(tái)django-suit的安裝配置操作
這篇文章主要介紹了django美化后臺(tái)django-suit的安裝配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Python數(shù)據(jù)分析之雙色球統(tǒng)計(jì)兩個(gè)紅和藍(lán)球哪組合比例高的方法
這篇文章主要介紹了Python數(shù)據(jù)分析之雙色球統(tǒng)計(jì)兩個(gè)紅和藍(lán)球哪組合比例高的方法,涉及Python數(shù)值運(yùn)算及圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
Python機(jī)器學(xué)習(xí)利用鳶尾花數(shù)據(jù)繪制ROC和AUC曲線(xiàn)
這篇文章主要為大家介紹了Python機(jī)器學(xué)習(xí)利用鳶尾花數(shù)據(jù)繪制ROC和AUC曲線(xiàn)實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

