鍍金池/ 教程/ Python/ 面向?qū)ο笤O(shè)計(jì)模式
反模式
隊(duì)列
適配器設(shè)計(jì)模式
享元設(shè)計(jì)模式
Python設(shè)計(jì)模式
工廠模式
模板設(shè)計(jì)模式
構(gòu)建器(Builder)設(shè)計(jì)模式
Python設(shè)計(jì)模式概要
命令設(shè)計(jì)模式
Python設(shè)計(jì)模式簡(jiǎn)介
觀察者設(shè)計(jì)模式
代理設(shè)計(jì)模式
異常處理
責(zé)任鏈設(shè)計(jì)模式
字典實(shí)現(xiàn)
抽象工廠設(shè)計(jì)模式
Python并發(fā)(多線程)
策略設(shè)計(jì)模式
門面(Facade)設(shè)計(jì)模式
原型設(shè)計(jì)模式
迭代器設(shè)計(jì)模式
集合
單例模式
列表數(shù)據(jù)結(jié)構(gòu)
狀態(tài)設(shè)計(jì)模式
模型視圖控制器(MVC)模式
裝飾器設(shè)計(jì)模式
面向?qū)ο蟾拍畹膶?shí)現(xiàn)
面向?qū)ο笤O(shè)計(jì)模式
字符串和序列化

面向?qū)ο笤O(shè)計(jì)模式

面向?qū)ο蟮哪J绞亲畛S玫哪J健?幾乎所有的編程語(yǔ)言都可以找到這種模式。

如何實(shí)現(xiàn)面向?qū)ο蟮哪J剑?/h2>

下面讓我們看看如何實(shí)現(xiàn)面向?qū)ο蟮哪J?。參考以下?shí)現(xiàn)代碼 -

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))

執(zhí)行上面示例代碼,得到以下輸出結(jié)果 -

說(shuō)明
代碼包括類屬性和實(shí)例屬性,它們按照輸出的要求打印。有各種功能構(gòu)成面向?qū)ο竽J降囊徊糠帧?這些功能在下一章中介紹。