轻松掌握python设计模式之策略模式,python设计模式
轻松掌握python设计模式之策略模式,python设计模式
本文实例为大家分享了python策略模式代码,供大家参考,具体内容如下
""" 策略模式 """ 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 + ' 从执行1') def execute_replacement2(self): print(self.name + ' 从执行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()
运行结果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持帮客之家。
相关内容
- Python基础中所出现的异常报错总结,python基础总结
- 【Python】Python的urllib模块、urllib2模块批量进行网页下载
- 独特的python循环语句,python语句
- Python 描述符(Descriptor)入门,pythondescriptor
- python制作websocket服务器实例分享,pythonwebsocket
- python中异常报错处理方法汇总,python处理方法汇总
- 详解python的数字类型变量与其方法,详解python
- 深入理解 Python 中的多线程 新手必看,python新手必看
- Python脚本实时处理log文件的方法,
- Python pass详细介绍及实例代码,pythonpass
评论关闭