網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Python中單類繼承
Python是一門(mén)面向?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 sweet
Python中多類繼承
在單類繼承中,super()函數(shù)用于指向要繼承的父類,且不需要顯式的寫(xiě)出父類名稱。
但是在多類繼承中,會(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'>)
原文鏈接:https://blog.csdn.net/w1301100424/article/details/93858890
相關(guān)推薦
- 2022-07-11 UVM中uvm_config_db在sequence中的使用
- 2022-05-06 mac brew 啟動(dòng)服務(wù)時(shí)報(bào)錯(cuò)“Bootstrap failed: 5: Input/output
- 2022-12-10 Qt中簡(jiǎn)單的按鈕槽函數(shù)傳遞參數(shù)方法_C 語(yǔ)言
- 2022-07-18 Linux文件權(quán)限
- 2022-08-20 解決python遞歸函數(shù)及遞歸次數(shù)受到限制的問(wèn)題_python
- 2022-12-12 python?使用?with?open()?as?讀寫(xiě)文件的操作方法_python
- 2023-10-25 更簡(jiǎn)單的方法實(shí)現(xiàn)el-calendar日歷組件中點(diǎn)擊上個(gè)月、今天、下個(gè)月按鈕時(shí)的點(diǎn)擊事件
- 2022-02-27 解決 idea突然使用debug功能時(shí)項(xiàng)目啟動(dòng)一半卡住沒(méi)反應(yīng)也不報(bào)錯(cuò)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支