輕松掌握python設(shè)計模式之策略模式
更新時間:2016年11月18日 15:52:03 作者:天風(fēng)隼
這篇文章主要幫助大家輕松掌握python設(shè)計模式之策略模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python策略模式代碼,供大家參考,具體內(nèi)容如下
""" 策略模式 """ import types class StrategyExample: def __init__(self, func=None): self.name = '策略例子0' if func is not None: """給實例綁定方法用的,不會影響到其他實例""" self.execute = types.MethodType(func, self) def execute(self): print(self.name) def execute_replacement1(self): print(self.name + ' 從執(zhí)行1') def execute_replacement2(self): print(self.name + ' 從執(zhí)行2') if __name__ == '__main__': strat0 = StrategyExample() strat1 = StrategyExample(execute_replacement1) strat1.name = '策略例子1' strat2 = StrategyExample(execute_replacement2) strat2.name = '策略例子2' strat0.execute() strat1.execute() strat2.execute()
運行結(jié)果如圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python使用opencv對圖像添加噪聲(高斯/椒鹽/泊松/斑點)
這篇文章主要介紹了python使用opencv對圖像添加噪聲(高斯/椒鹽/泊松/斑點),具有一定的學(xué)習(xí)價值,需要的小伙伴可以參考一下,希望對你有所幫助2022-04-04python使用OpenCV獲取高動態(tài)范圍成像HDR
這篇文章主要介紹了python使用OpenCV獲取高動態(tài)范圍成像HDR,如何使用不同曝光設(shè)置拍攝的多張圖像創(chuàng)建高動態(tài)范圍圖像HDR,下文嗎更詳細的內(nèi)容介紹,需要的小伙伴可以參考一下2022-04-04python+matplotlib實現(xiàn)禮盒柱狀圖實例代碼
這篇文章主要介紹了python+matplotlib實現(xiàn)禮盒柱狀圖實例代碼,具有一定借鑒價值,需要的朋友可以參考下2018-01-01TensorFlow神經(jīng)網(wǎng)絡(luò)構(gòu)造線性回歸模型示例教程
這篇文章主要為大家介紹了TensorFlow構(gòu)造線性回歸模型示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11