跳到主要內容

發表文章

目前顯示的是有「Python物件導向」標籤的文章

[Python物件導向]Python封裝(Encapsulation)實用教學

Photo by Dayne Topkin on Unsplash 今天要來介紹Python物件導向設計中的 封裝 (Encapsulation) 概念, 主要的目的在保護程式碼中重要的實作細節不被外部知道,以防止外部程式碼直接或不當的存取類別中的屬性 (Attribute) 及方法 (Method) ,而導致程式邏輯上的錯誤。 將複雜的運算邏輯進行封裝 (Encapsulation) 的好處是,外部程式碼只要透過統一介面來進行存取即可,而無需瞭解其中的運算過程,讓程式碼除了保有存取上的控制外,也易於維護。 今天要來說明的 封裝 (Encapsulation) 重點包含了: Python 封裝 (Encapsulation) 私有屬性 (Private Attribute) 私有方法 (Private Method) 一、 Python 封裝 (Encapsulation) 就如同文章開頭所說明的,封裝 (Encapsulation) 的概念就有點像權限控制,有些屬性 (Attribute) 或方法 (Method) 只想於類別內部使用,而不想公開於外部,除了減少程式碼因來源端不適當的使用發生問題外,也可保護其中重要的商業邏輯。 一般情況下,來源端皆可存取類別中公開的屬性 (Attribute) 及方法 (Method) ,如下範例: # 部落格類別 class Blog: # 建構式 def __init__(self): self.author = "Mike" # 作者屬性 self.titles = [] # 文章標題屬性 # 新增文章 def add_post(self, title): self.titles.append(title) blog = Blog() blog.add_post("Python Programming") blog.add_post("Learn How to Code") print("Author: ", blog.author) print("Blog Titles: ", blo...

[Python物件導向]Python多型(Polymorphism)實用教學

Photo by Plush Design Studio on Unsplash 今天要來介紹的 Python 多型 (Polymorphism) 在物件導向設計中非常的重要,不論是設計模式 (Design Patterns) 或設計原則 (Design Principles) ,都會有 多型 (Polymorphism) 的概念。 使用多型 (Polymorphism) 來設計類別架構,能夠讓程式碼的相依性不會那麼高,並且透過 統一的介面來彈性 擴充功能。今天主要的重點有兩個部分: Python 抽象方法 (Abstract Method) 多型 (Polymorphism) 一、 Python 抽象方法 (Abstract Method) 要使用抽像方法 (Abstract Method) 的類別首先要繼承 ABC(Abstract Base Class) 類別,接著在抽象方法上方加上 @abstractmethod 裝飾詞 (Decorator) ,並且不會有實作內容 ,如下範例: from abc import ABC, abstractmethod # 登入類別 class Login(ABC): @abstractmethod def login(self): pass 由於抽象方法 (Abstract Method) 是抽象的,所以只要有抽象方法 (Abstract Method) 的類別就稱為抽象類別,是無法建立物件的,如下範例: from abc import ABC, abstractmethod # 登入類別 class Login(ABC): @abstractmethod def login(self): pass user_login = Login() 執行結果 錯誤訊息顯示無法實體化含有抽象方法 (Abstract Method) 的抽象類別。必須 透過繼承 (Inheritance) 的類別來進行抽象方法 (Abstract Method) 的實作,如下範例: from abc import ABC, abstractmethod # 登入抽象類別 class Login(ABC): ...

[Python物件導向]Python繼承(Inheritance)實用教學

Photo by David Travis on Unsplash 今天要來介紹的 Python 繼承 (Inheritance) 在物件導向設計中非常的重要,使用得當可以提高我們程式碼的重用性 (Reusable) 及維護性。 繼承 (Inheritance) 顧名思義,就是會有父類別(或稱基底類別 Base Class) 及子類別 (Sub Class) 的階層關係。子類別會擁有父類別公開的屬性 (Attribute) 及方法 (Method) 。 所以 Python 繼承 (Inheritance) 的概念就是 將各類別 (Class) 會共同使用的屬性 (Attribute) 或方法 (Method) 放在一個獨立的類別 (Class) 中,其它的類別 (Class)透過 繼承 (Inheritance) 的方式來擁有,降低程式碼的重複性。 Python 繼承 (Inheritance) 的重要觀念如下: 如何使用 Python 繼承 (Inheritance) 方法覆寫 (Method Overriding) 多層繼承 (Multi-Level Inheritance) 多重繼承 (Multiple Inheritance) 一、如何使用Python繼承 (Inheritance) 在實務上開發應用程式時,隨著類別 (Class) 的增加,可能會發現有些類別 (Class) 擁有共同的 屬性 (Attribute) 或 方法 (Method) ,如下範例: # 汽車類別 class Car: # 駕駛方法 def drive(self): print("drive method is called.") # 加速方法 def accelerate(self): print("accelerate method is called.") # 飛機類別 class Airplane: # 駕駛方法 def drive(self): print("drive method is called.") # 飛行方法 def ...

[Python物件導向]解析Python物件導向設計的3種類型方法(Instance,Class,Static Method)

Photo by AbsolutVision on Unsplash 在 淺談Python類別(Class) 的文章最後有提到物件導向設計的方法類型有分為三種,嚴格來說還有第四種抽象方法 (Abstract Method) ,這個待介紹 Python 多型 (Polymorphism) 設計時,再來詳細介紹。 今天要說明的以下三種方法 (Method) 在物件導向設計(Object-oriented programming)中非常的重要, 瞭解其 觀念及應用有助於 類別 (Class) 的設計更有彈性且易於維護。 實體方法 (Instance Method) 類別方法 (Class Method) 靜態方法 (Static Method) 一、實體方法 (Instance Method) Python類別(Class)中 沒有加任何裝飾詞 (Decorator) 的方法 (Method) , 至少要有一個 self 參數,於方法 (Method) 被呼叫時指向物件 (Object) ,其後可以依需求增加額外參數,如下範例: #汽車類別 class Cars: #實體方法(Instance Method) def drive(self): print("Drive is instance method.") 實體方法 (Instance Method) 透過 self 參數可以自由的存取物件 (Object) 的屬性 (Attribute) 及其他方法 (Method) ,藉此來改變物件 (Object) 的狀態,如下範例: # 汽車類別 class Cars: # 建構式 def __init__(self): self.color = "blue" # 實體方法(Instance Method) def drive(self): print(f"{self} is {self.color}.") self.message() # 呼叫其他方法 # 實體方法(Instance Method) def message(self...