{"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 <\/head>
\n <\/p>\n

\n

Python\u72b6\u6001\u6a21\u5f0f<\/h1>\n

Python\u72b6\u6001\u6a21\u5f0f\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u5b83\u4e3a\u72b6\u6001\u673a\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6a21\u5757\uff0c\u8be5\u6a21\u5757\u4f7f\u7528\u4ece\u6307\u5b9a\u72b6\u6001\u673a\u7c7b\u6d3e\u751f\u7684\u5b50\u7c7b\u5b9e\u73b0\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0e\u72b6\u6001\u65e0\u5173\uff0c\u5e76\u5bfc\u81f4\u4f7f\u7528\u88c5\u9970\u5668\u58f0\u660e\u8f6c\u6362\u3002\n <\/div>\n

\u5982\u4f55\u5b9e\u73b0\u72b6\u6001\u6a21\u5f0f\uff1f<\/h2>\n
\n \u72b6\u6001\u6a21\u5f0f\u7684\u57fa\u672c\u5b9e\u73b0\u5982\u4e0b\u6240\u793a-\n <\/div>\n
\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
\n \u4e0a\u9762\u7684\u7a0b\u5e8f\u751f\u6210\u4ee5\u4e0b\u8f93\u51fa-\n <\/div>\n
\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

<\/body>
\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}]}}