|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
标题其实么没有确切描述我的问题。我是在博客园看见的一个关于用python实现设计模式的帖子。具体代码如下:
class Work:
def __init__(self):
self.hour = 9
self.current = ForenoonState()
def SetState(self,temp):
self.current = temp
def WriteProgram(self):
self.current.WriteProgram(self)
class NoonState(State):
def WriteProgram(self,w):
print "noon working"
if (w.hour<13):
print "fun."
else:
print "need to rest."
class ForenoonState(State):
def WriteProgram(self,w):
if (w.hour<12):
print "morning working"
print "energetic"
else:
w.SetState(NoonState())
w.WriteProgram()
if __name__ == "__main__":
mywork = Work()
mywork.hour = 9
mywork.WriteProgram()
mywork.hour =14
mywork.WriteProgram()
我看不明白的地方在于 类 ForenoonState 的 WriteProgram 方法 有一个 w 参数。从代码运行的结果来看 w 应该是 mywork 这个 Work 的实例类。但这是何时赋值上去的呢。还是说这里有什么隐藏语法是我所不知道的。求知道的人指教,如果能回复更多类似的“隐藏于法”那就太感谢了。谢谢。
引用来源:
http://www.cnblogs.com/wuyuegb23 ... /04/09/3008320.html
版主: 五岳
|
|