{"id":2117,"date":"2023-11-10T15:10:41","date_gmt":"2023-11-10T07:10:41","guid":{"rendered":""},"modified":"2023-11-10T15:10:41","modified_gmt":"2023-11-10T07:10:41","slug":"Python\u72b6\u6001\u6a21\u5f0f","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/2117.html","title":{"rendered":"Python\u72b6\u6001\u6a21\u5f0f"},"content":{"rendered":"
\n
Python\u72b6\u6001\u6a21\u5f0f\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n <\/body>\u5982\u4f55\u5b9e\u73b0\u72b6\u6001\u6a21\u5f0f\uff1f<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
class <\/span>ComputerState(object):
name = \"state\"<\/span>
allowed = []
def <\/span>switch(self, state):
\"\"<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>\" Switch to new state \"<\/span>\"\"
if <\/span>state.name in <\/span>self.allowed:
print('Current:'<\/span><\/span>,self,' => switched to new state'<\/span>,state.name)
self.__class__ = state
else:<\/span>
print('Current:',self,' => switching to'<\/span>,state.name,'not possible.'<\/span>)
def <\/span>__str__(self):
return <\/span>self.name
class <\/span>Off(ComputerState):
name = \"off\"<\/span>
allowed = ['on'<\/span><\/span><\/span>]
class <\/span>On(ComputerState):
\"\"\" State of being powered on and <\/span>working \"<\/span>\"\"
name = \"on\"<\/span>
allowed = ['off'<\/span>,'suspend'<\/span>,'hibernate'<\/span>]
class <\/span>Suspend(ComputerState):
\"\"\" State of being in <\/span>suspended mode after switched on \"<\/span>\"\"
name = \"suspend\"<\/span>
allowed = ['on']
class <\/span>Hibernate(ComputerState):
\"\"\" State of being in <\/span>hibernation after powered on \"<\/span>\"\"
name = \"hibernate\"<\/span>
allowed = ['on']
class <\/span>Computer(object):
\"\"\" A class <\/span>representing a computer \"<\/span>\"\"
def <\/span>__init__(self, model='HP'<\/span>):
self.model <\/span>= model
# State of the computer - default is <\/span>off.
<\/span> self.state = Off<\/span>()
def <\/span>change(self, state):
\"\"\" Change state \"<\/span>\"\"
self.state.switch<\/span>(state)
if <\/span>__name__ == \"__main__\"<\/span>:
comp = Computer()
comp.change<\/span>(On)
comp.change<\/span>(Off)
comp.change<\/span>(On)
comp.change<\/span>(Suspend)
comp.change<\/span>(Hibernate)
comp.change<\/span>(On)
comp.change<\/span>(Off)
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
Current: off => switched to new state on \n
Current: on => switched to new state off \n
Current: off => switched to new state on \n
Current: on => switched to new state suspend \n
Current: suspend => switching to hibernate not possible. \n
Current: suspend => switched to new state on \n
Current: on => switched to new state off<\/code><\/pre>\n<\/p><\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u72b6\u6001\u6a21\u5f0fzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[164],"tags":[],"class_list":["post-2117","post","type-post","status-publish","format-standard","hentry","category-python-sjms"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/2117"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=2117"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/2117\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=2117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=2117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=2117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}