
- 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 設計模式-面向物件
面向物件的模式是最常用的模式。幾乎在所有程式語言中都可以找到此模式。
如何實現面向物件的模式?
現在讓我們看看如何實現面向物件的模式。
class Parrot: # class attribute species = "bird" # instance attribute def __init__(self, name, age): self.name = name self.age = age # instantiate the Parrot class blu = Parrot("Blu", 10) woo = Parrot("Woo", 15) # access the class attributes print("Blu is a {}".format(blu.__class__.species)) print("Woo is also a {}".format(woo.__class__.species)) # access the instance attributes print("{} is {} years old".format( blu.name, blu.age)) print("{} is {} years old".format( woo.name, woo.age))
輸出
以上程式生成以下輸出

說明
該程式碼包括類屬性和例項屬性,它們會根據輸出的要求進行列印。面向物件的模式由各種特性組成。下一章將對這些特性進行說明。
廣告