日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python類繼承及super()函數使用說明_python

作者:waifdzdn ? 更新時間: 2022-12-22 編程語言

Python中單類繼承

Python是一門面向對象的編程語言,支持類繼承。

新的類稱為子類(Subclass),被繼承的類稱為父類、基類或者超類。子類繼承父類后,就擁有父類的所有特性。

類繼承的簡單例子:

普通類方法繼承

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

子類可以在繼承父類方法的同時,對方法進行重構。

這樣一來,子類的方法既包含父類方法的特性,同時也包含子類自己的特性:

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

初始化函數繼承

如果我們需要給類傳入參數,需要使用初始化函數。如果所有子類中部分參數是相同的,那么可以在父類的初始化函數中定義這些參數,然后子類繼承父類的初始化函數,這樣所有子類就可共享這些參數,而不需要在每個子類中單獨定義。

初始化函數的繼承:

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) # 等價于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 sweet

Python中多類繼承

在單類繼承中,super()函數用于指向要繼承的父類,且不需要顯式的寫出父類名稱。

但是在多類繼承中,會涉及到查找順序(MRO)、鉆石繼承等問題。

MRO 是類的方法解析順序表, 也就是繼承父類方法時的順序表。

鉆石繼承

? ? A
? ?/ \
? B ? C
? ?\ /
? ? D

如圖所示,A是父類,B和C繼承A,D繼承B和C。

下面舉例說明鉆石繼承的繼承順序

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'>)

原文鏈接:https://blog.csdn.net/w1301100424/article/details/93858890

欄目分類
最近更新