python_类的设计模式
来源:互联网 发布:java 证书导出私钥 编辑:程序博客网 时间:2024/05/19 13:16
策略模式class Moveable: def move(self): print("move...")class Moveonfeet: def move(self): print("move on feet...")class Moveonwheel: def move(self): print("move on wheel...")class MoveObj: def set_move(self,moveable): self.moveable = moveable() def move(self): self.moveable.move()class Test: def move(self): print("I'm Fly.")if __name__ =='__main__': m=MoveObj() m.set_move(Moveable) m.move() m.set_move(Moveonfeet) m.move() m.set_move(Moveonwheel) m.move() m.set_move(Test) m.move()
def Movea(): print("move a...")def Moveb(): print("move b...")class MoveObj: def set_move(self,moveable): self.moveable = moveable #() def move(self): self.moveable() #()if __name__ =='__main__': m=MoveObj() m.set_move(Movea) m.move() m.set_move(Moveb) m.move()
#装饰模式class Tree: def start(self): print("tree a ...") def mid(self): print("tree b...")class Decorater: def __init__(self,T): self.t=T() def add_start(self): print("start first add...") self.t.start() def add_mid(self): print("start second add...") self.t.start()if __name__ == '__main__': tree=Tree() tree.start() tree.mid() dec=Decorater(Tree) dec.add_start() dec.add_mid()
def Decorater(a_class): class NewClass: def __init__(self,age,color): self.old = a_class(age) self.color = color def display(self): print(self.color) print(self.old.age) #!!! return NewClass@Decoraterclass Dog: def __init__(self,age): self.age = age def display(self): print(self.age)if __name__ == '__main__': dog=Dog(12,'red') print(dog.display())
1 0
- python_类的设计模式
- python_类的继承
- Python_类的继承
- python_设计模式——责任链模式
- python_类的一些方法
- 22-python_类的定义
- 23-python_类的方法
- python_类的实例化
- python_类
- Python_类
- python_单例模式
- python_单例模式
- python_文件的读取
- python_文件的读写
- python_文件的处理
- python_文件的copy
- python_文件的赋值
- python_类包装
- 95,autorelease的注意事项
- 托盘图标的制作,点击与触发
- 求两个时间差的天数、小时、分钟、秒
- android popupwindow的使用以及点击空白处消失 以及Dialog框的使用
- 也谈exe文件关联修复
- python_类的设计模式
- 多盟积分墙sdk的使用
- CRefCount
- centos 7.0 java+tomcat+mysql 环境搭建
- Josephus环
- java.lang.UnsatisfiedLinkError: Native Library * already loaded in another classloader
- rbenv-install-and-using
- 第13周项目1 算法验证—Prim算法+Kruskal算法
- entity framework的使用(自动生成数据库模型)