Home
Categories
WIKI
Topic
User
LANGUAGE:
中文
English
简单的策略模式
Experiences and Insight
703
views ·
1
replies ·
To
floor
Go
z85525006
deepin
2012-06-13 21:18
Author
策略模式是对算法的一种封装。
class Opertion(object):
def oper(a, b):
pass
class Sub(object):
def oper(self,a, b):
return a - b
class Div(object):
def oper(self,a, b):
return float(a) / b
class Add(object):
def oper(self,a, b):
return a + b
class Cls(object):
def __init__(self, oper):
self.op = oper
def oper(self, a, b):
return self.op.oper(a, b)
if __name__ == "__main__":
cls = Cls(Add())
print cls.oper(3, 4)
cls = Cls(Sub())
print cls.oper(3, 4)
cls = Cls(Div())
print cls.oper(3, 4)
Copy the Code
Reply
Like 0
Favorite
View the author
All Replies
cnchanghai
deepin
2012-06-14 04:40
#1
这个有啥用
Reply
Like 0
View the author
Please
sign
in first
New Thread
Popular Ranking
Change
Instructions on How to change the language in Thunderbird
deepin eighth Bi-Weekly Technical Report is online
Popular Events
More