
- Python 基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World 程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境搭建
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode 系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python 控制語句
- Python - 控制流
- Python - 決策
- Python - if 語句
- Python - if else
- Python - 巢狀 if
- Python - match-case 語句
- Python - 迴圈
- Python - for 迴圈
- Python - for-else 迴圈
- Python - while 迴圈
- Python - break 語句
- Python - continue 語句
- Python - pass 語句
- Python - 巢狀迴圈
- Python 函式與模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 只接受關鍵字引數
- Python - 位置引數
- Python - 只接受位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python 字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python 列表
- Python - 列表
- Python - 訪問列表元素
- Python - 修改列表元素
- Python - 新增列表元素
- Python - 刪除列表元素
- Python - 迴圈遍歷列表
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python 元組
- Python - 元組
- Python - 訪問元組元素
- Python - 更新元組
- Python - 解包元組
- Python - 迴圈遍歷元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python 集合
- Python - 集合
- Python - 訪問集合元素
- Python - 新增集合元素
- Python - 刪除集合元素
- Python - 迴圈遍歷集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python 字典
- Python - 字典
- Python - 訪問字典元素
- Python - 修改字典元素
- Python - 新增字典元素
- Python - 刪除字典元素
- Python - 字典檢視物件
- Python - 迴圈遍歷字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python 陣列
- Python - 陣列
- Python - 訪問陣列元素
- Python - 新增陣列元素
- Python - 刪除陣列元素
- Python - 迴圈遍歷陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python 檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS 檔案/目錄方法
- Python - OS 路徑方法
- 面向物件程式設計
- Python - OOPs 概念
- Python - 類與物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝類
- Python - 列舉
- Python - 反射
- Python 錯誤與異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except 塊
- Python - try-finally 塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀 try 塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python 多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 執行緒連線
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python 同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python 網路程式設計
- Python - 網路程式設計
- Python - Socket 程式設計
- Python - URL 處理
- Python - 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 雜項
- Python - 日期與時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI 程式設計
- Python - XML 處理
- Python - GUI 程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 更多擴充套件
- Python - 工具/實用程式
- Python - GUIs
- Python 高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - Monkey Patching
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python 有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
Python - 繼承
什麼是 Python 中的繼承?
繼承是像 Python 這樣的面向物件程式語言最重要的特性之一。它用於將一個類的屬性和行為繼承到另一個類。繼承另一個類的類稱為子類,被繼承的類稱為基類或父類。
如果你必須設計一個新類,而它的大部分屬性已經在現有類中定義得很好了,那麼為什麼要重新定義它們呢?繼承允許重用現有類的功能,如果需要,可以擴充套件它們來設計新類。
當新類與現有類具有“IS A”關係時,就會出現繼承。例如,汽車是車輛,公共汽車是車輛,腳踏車也是車輛。這裡,車輛是父類,而汽車、公共汽車和腳踏車是子類。

建立父類
屬性和方法被繼承的類稱為父類。它的定義方式與其他類一樣,即使用 class 關鍵字。
語法
建立父類的語法如下所示:
class ParentClassName: {class body}
建立子類
從基類繼承的類與它們的父類的宣告方式類似,但是,我們需要在括號內提供父類的名稱。
語法
以下是子類的語法:
class SubClassName (ParentClass1[, ParentClass2, ...]): {sub class body}
繼承的型別
在 Python 中,繼承可以分為五類:
- 單繼承
- 多繼承
- 多層繼承
- 層次繼承
- 混合繼承

Python - 單繼承
這是最簡單的繼承形式,其中子類只從一個父類繼承屬性和方法。
示例
下面的示例展示了 Python 中的單繼承概念:
# parent class class Parent: def parentMethod(self): print ("Calling parent method") # child class class Child(Parent): def childMethod(self): print ("Calling child method") # instance of child c = Child() # calling method of child class c.childMethod() # calling method of parent class c.parentMethod()
執行上述程式碼後,將列印以下結果:
Calling child method Calling parent method
Python - 多繼承
Python 中的多繼承允許你基於多個父類構建一個類。因此,子類繼承了所有父類的屬性和方法。子類可以覆蓋從任何父類繼承的方法。
語法
class parent1: #statements class parent2: #statements class child(parent1, parent2): #statements
示例
Python 的標準庫有一個內建的 divmod() 函式,它返回一個包含兩項的元組。第一個數字是兩個引數的除法結果,第二個是兩個運算元的模值。
此示例嘗試模擬 divmod() 函式。我們定義了兩個類 division 和 modulus,然後有一個 div_mod 類繼承它們。
class division: def __init__(self, a,b): self.n=a self.d=b def divide(self): return self.n/self.d class modulus: def __init__(self, a,b): self.n=a self.d=b def mod_divide(self): return self.n%self.d class div_mod(division,modulus): def __init__(self, a,b): self.n=a self.d=b def div_and_mod(self): divval=division.divide(self) modval=modulus.mod_divide(self) return (divval, modval)
子類有一個新的方法 div_and_mod(),它內部呼叫其繼承類的 divide() 和 mod_divide() 方法來返回除法和模值。
x=div_mod(10,3) print ("division:",x.divide()) print ("mod_division:",x.mod_divide()) print ("divmod:",x.div_and_mod())
輸出
division: 3.3333333333333335 mod_division: 1 divmod: (3.3333333333333335, 1)
方法解析順序 (MRO)
術語方法解析順序與 Python 中的多繼承有關。在 Python 中,繼承可能跨越多個級別。假設 A 是 B 的父類,B 是 C 的父類。C 類可以覆蓋繼承的方法,或者它的物件可以呼叫在其父類中定義的方法。那麼,Python 如何找到要呼叫的適當方法呢?
每個 Python 都有一個 mro() 方法,它返回 Python 用於解析要呼叫的方法的層次順序。解析順序是從繼承順序的底部到頂部。
在我們之前的示例中,div_mod 類繼承了 division 和 modulus 類。因此,mro 方法返回的順序如下:
[<class '__main__.div_mod'>, <class '__main__.division'>, <class '__main__.modulus'>, <class 'object'>]
Python - 多層繼承
在多層繼承中,一個類派生自另一個派生類。存在多層繼承關係。我們可以將其想象為祖父母-父母-子女的關係。
示例
在下面的例子中,我們將演示多層繼承的工作方式。
# parent class class Universe: def universeMethod(self): print ("I am in the Universe") # child class class Earth(Universe): def earthMethod(self): print ("I am on Earth") # another child class class India(Earth): def indianMethod(self): print ("I am in India") # creating instance person = India() # method calls person.universeMethod() person.earthMethod() person.indianMethod()
執行以上程式碼後,將產生以下結果:
I am in the Universe I am on Earth I am in India
Python - 層次繼承
這種型別的繼承包含多個派生類,它們繼承自單個基類。這類似於組織內部的層次結構。
示例
下面的例子說明了層次繼承。在這裡,我們定義了Manager類的兩個子類。
# parent class class Manager: def managerMethod(self): print ("I am the Manager") # child class class Employee1(Manager): def employee1Method(self): print ("I am Employee one") # second child class class Employee2(Manager): def employee2Method(self): print ("I am Employee two") # creating instances emp1 = Employee1() emp2 = Employee2() # method calls emp1.managerMethod() emp1.employee1Method() emp2.managerMethod() emp2.employee2Method()
執行上述程式後,您將獲得以下輸出:
I am the Manager I am Employee one I am the Manager I am Employee two
Python - 混合繼承
兩種或多種繼承型別的組合稱為混合繼承。例如,它可能是單繼承和多繼承的混合。
示例
在這個例子中,我們結合了單繼承和多繼承,形成了類的混合繼承。
# parent class class CEO: def ceoMethod(self): print ("I am the CEO") class Manager(CEO): def managerMethod(self): print ("I am the Manager") class Employee1(Manager): def employee1Method(self): print ("I am Employee one") class Employee2(Manager, CEO): def employee2Method(self): print ("I am Employee two") # creating instances emp = Employee2() # method calls emp.managerMethod() emp.ceoMethod() emp.employee2Method()
執行上述程式後,將給出以下結果:
I am the Manager I am the CEO I am Employee two
super() 函式
在 Python 中,super() 函式允許您從子類中訪問父類的方法和屬性。
示例
在下面的例子中,我們建立一個父類,並使用 super() 函式從子類訪問其建構函式。
# parent class class ParentDemo: def __init__(self, msg): self.message = msg def showMessage(self): print(self.message) # child class class ChildDemo(ParentDemo): def __init__(self, msg): # use of super function super().__init__(msg) # creating instance obj = ChildDemo("Welcome to Tutorialspoint!!") obj.showMessage()
執行後,上述程式將給出以下結果:
Welcome to Tutorialspoint!!