選單
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Python 教程

Python 主頁 Python 簡介 Python 入門 Python 語法 Python 註釋 Python 變數 Python 資料型別 Python 數字 Python 型別轉換 Python 字串 Python 布林值 Python 運算子 Python 列表 Python 元組 Python 集合 Python 字典 Python If...Else Python While 迴圈 Python For 迴圈 Python 函式 Python Lambda Python 陣列 Python 類/物件 Python 繼承 Python 迭代器 Python 多型 Python 作用域 Python 模組 Python 日期 Python 數學 Python JSON Python 正則表示式 Python PIP Python Try...Except Python 使用者輸入 Python 字串格式化

檔案處理

Python 檔案處理 Python 讀取檔案 Python 寫入/建立檔案 Python 刪除檔案

Python 模組

NumPy 教程 Pandas 教程 SciPy 教程 Django 教程

Python Matplotlib

Matplotlib 簡介 Matplotlib 入門 Matplotlib Pyplot Matplotlib 繪圖 Matplotlib 標記 Matplotlib 線條 Matplotlib 標籤 Matplotlib 網格 Matplotlib 子圖 Matplotlib 散點圖 Matplotlib 條形圖 Matplotlib 直方圖 Matplotlib 餅圖

機器學習

入門 均值、中位數、眾數 標準差 百分位數 資料分佈 正態資料分佈 散點圖 線性迴歸 多項式迴歸 多元迴歸 縮放 訓練/測試 決策樹 混淆矩陣 層次聚類 邏輯迴歸 網格搜尋 分類資料 K-means Bootstrap Aggregation 交叉驗證 AUC - ROC 曲線 K-近鄰

Python MySQL

MySQL 入門 MySQL 建立資料庫 MySQL 建立表 MySQL 插入 MySQL 選擇 MySQL Where MySQL Order By MySQL 刪除 MySQL 刪除表 MySQL 更新 MySQL Limit MySQL Join

Python MongoDB

MongoDB 入門 MongoDB 建立資料庫 MongoDB 集合 MongoDB 插入 MongoDB Find MongoDB Query MongoDB Sort MongoDB 刪除 MongoDB 刪除集合 MongoDB 更新 MongoDB Limit

Python 參考

Python 概述 Python 內建函式 Python 字串方法 Python 列表方法 Python 字典方法 Python 元組方法 Python 集合方法 Python 檔案方法 Python 關鍵字 Python 異常 Python 詞彙表

模組參考

Random 模組 Requests 模組 Statistics 模組 Math 模組 cMath 模組

Python 如何操作

刪除列表重複項 反轉字串 兩個數字相加

Python 示例

Python 示例 Python 編譯器 Python 練習 Python 測驗 Python 伺服器 Python 面試問答 Python 訓練營 Python 證書

Python 多型


“多型”這個詞的意思是“多種形式”,在程式設計中,它指的是具有相同名稱的方法/函式/運算子,可以對許多物件或類執行。


函式多型

一個可以用於不同物件的 Python 函式的例子是 len() 函式。

字串

對於字串,len() 返回字元數

示例

x = "Hello World!"

print(len(x))
自己動手試一試 »

元組

對於元組,len() 返回元組中的項數

示例

mytuple = ("apple", "banana", "cherry")

print(len(mytuple))
自己動手試一試 »

字典

對於字典,len() 返回字典中鍵/值對的數量

示例

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

print(len(thisdict))
自己動手試一試 »


類多型

多型經常用於類方法中,我們可以讓多個類擁有相同名稱的方法。

例如,假設我們有三個類:CarBoatPlane,它們都具有一個名為 move() 的方法。

示例

具有相同方法的不同類

class Car
  def __init__(self, brand, model)
    self.brand = brand
    self.model = model

  def move(self)
    print("Drive!")

class Boat
  def __init__(self, brand, model)
    self.brand = brand
    self.model = model

  def move(self)
    print("Sail!")

class Plane
  def __init__(self, brand, model)
    self.brand = brand
    self.model = model

  def move(self)
    print("Fly!")

car1 = Car("Ford", "Mustang")       #建立 Car 類
boat1 = Boat("Ibiza", "Touring 20") #建立 Boat 類
plane1 = Plane("Boeing", "747")     #建立 Plane 類

for x in (car1, boat1, plane1)
  x.move()
自己動手試一試 »

看看最後的 for 迴圈。由於多型,我們可以對所有三個類執行相同的方法。


繼承類多型

擁有同名子類的類呢?我們可以在那裡使用多型嗎?

是的。如果我們使用上面的例子,建立一個名為 Vehicle 的父類,並將 CarBoatPlane 類設為 Vehicle 的子類,子類將繼承 Vehicle 的方法,但可以重寫它們。

示例

建立一個名為 Vehicle 的類,並將 CarBoatPlane 類設為 Vehicle 的子類。

class Vehicle
  def __init__(self, brand, model)
    self.brand = brand
    self.model = model

  def move(self)
    print("Move!")

class Car(Vehicle)
  pass

class Boat(Vehicle)
  def move(self)
    print("Sail!")

class Plane(Vehicle)
  def move(self)
    print("Fly!")

car1 = Car("Ford", "Mustang") #建立 Car 物件
boat1 = Boat("Ibiza", "Touring 20") #建立 Boat 物件
plane1 = Plane("Boeing", "747") #建立 Plane 物件

for x in (car1, boat1, plane1)
  print(x.brand)
  print(x.model)
  x.move()
自己動手試一試 »

子類繼承了父類的屬性和方法。

在上面的例子中,您可以看到 Car 類是空的,但它從 Vehicle 繼承了 brandmodelmove()

BoatPlane 類也從 Vehicle 繼承了 brandmodelmove(),但它們都重寫了 move() 方法。

由於多型,我們可以對所有類執行相同的方法。



×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援