{"id":2111,"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\u547d\u4ee4\u6a21\u5f0f","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/2111.html","title":{"rendered":"Python\u547d\u4ee4\u6a21\u5f0f"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python\u547d\u4ee4\u6a21\u5f0f<\/h1>\n

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

\n \u547d\u4ee4\u6a21\u5f0f\u5728\u52a8\u4f5c\u4e4b\u95f4\u6dfb\u52a0\u4e86\u4e00\u4e2a\u62bd\u8c61\u7ea7\u522b\uff0c\u5e76\u5305\u62ec\u4e00\u4e2a\u8c03\u7528\u8fd9\u4e9b\u52a8\u4f5c\u7684\u5bf9\u8c61\u3002\n <\/div>\n
\n \u5728\u6b64\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\uff0c\u5ba2\u6237\u7aef\u521b\u5efa\u4e00\u4e2a\u547d\u4ee4\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u8981\u6267\u884c\u7684\u547d\u4ee4\u5217\u8868\u3002\u521b\u5efa\u7684\u547d\u4ee4\u5bf9\u8c61\u5b9e\u73b0\u7279\u5b9a\u7684\u63a5\u53e3\u3002\n <\/div>\n
\n \u4ee5\u4e0b\u662f\u547d\u4ee4\u6a21\u5f0f\u7684\u57fa\u672c\u67b6\u6784-\n <\/div>\n

Python\u547d\u4ee4\u6a21\u5f0f_https:\/\/bianchenghao6.com_\u3010Python \u8bbe\u8ba1\u6a21\u5f0f\u3011_\u7b2c1\u5f20 <\/p>\n

\u5982\u4f55\u5b9e\u73b0\u547d\u4ee4\u6a21\u5f0f\uff1f<\/h2>\n
\n \u6211\u4eec\u73b0\u5728\u5c06\u4e86\u89e3\u5982\u4f55\u5b9e\u73b0\u8bbe\u8ba1\u6a21\u5f0f\u3002\n <\/div>\n
\n \u5728\u5927\u591a\u6570\u996d\u5e97\u4e2d\uff0c\u5f53\u670d\u52a1\u5458\u5df2\u7ecf\u63a5\u5230\u987e\u5ba2\u7684\u70b9\u5355\uff0c\u5f55\u5165\u5230\u7cfb\u7edf\u4e2d\u540e\uff0c\u6839\u636e\u4e0d\u540c\u7684\u83dc\u54c1\uff0c\u4f1a\u6709\u4e0d\u540c\u7684\u540e\u53f0\u53cd\u5e94\u3002\u6bd4\u5982\uff0c\u996d\u5e97\u6709\u51c9\u83dc\u95f4\u3001\u70ed\u83dc\u95f4\u3001\u4e3b\u98df\u95f4\uff0c\u90a3\u5f53\u670d\u52a1\u5458\u5c06\u83dc\u54c1\u5f55\u5165\u5230\u7cfb\u7edf\u4e2d\u540e\uff0c\u51c9\u83dc\u95f4\u4f1a\u6253\u5370\u51fa\u987e\u5ba2\u6240\u70b9\u7684\u51c9\u83dc\u6761\u76ee\uff0c\u70ed\u83dc\u95f4\u4f1a\u6253\u5370\u51fa\u987e\u5ba2\u6240\u70b9\u7684\u70ed\u83dc\u6761\u76ee\uff0c\u4e3b\u98df\u95f4\u4f1a\u6253\u5370\u51fa\u4e3b\u98df\u6761\u76ee\u3002\u90a3\u8fd9\u4e2a\u7cfb\u7edf\u7684\u540e\u53f0\u6a21\u5f0f\u8be5\u5982\u4f55\u8bbe\u8ba1\uff1f\u5f53\u7136\uff0c\u76f4\u63a5\u5728\u573a\u666f\u4ee3\u7801\u4e2d\u52a0if\u2026else\u2026\u8bed\u53e5\u5224\u65ad\u662f\u4e2a\u65b9\u6cd5\uff0c\u53ef\u8fd9\u6837\u505a\u53c8\u4e00\u6b21\u52a0\u91cd\u4e86\u7cfb\u7edf\u8026\u5408\uff0c\u8fdd\u53cd\u4e86\u5355\u4e00\u804c\u8d23\u539f\u5219\uff0c\u9047\u5230\u7cfb\u7edf\u9700\u6c42\u53d8\u52a8\u65f6\uff0c\u53c8\u4f1a\u8f7b\u6613\u8fdd\u53cd\u5f00\u95ed\u539f\u5219\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u9700\u8981\u91cd\u65b0\u7ec4\u7ec7\u4e00\u4e0b\u7ed3\u6784\u3002\u53ef\u4ee5\u5c06\u8be5\u7cfb\u7edf\u8bbe\u8ba1\u6210\u524d\u53f0\u670d\u52a1\u5458\u7cfb\u7edf\u548c\u540e\u53f0\u7cfb\u7edf\uff0c\u540e\u53f0\u7cfb\u7edf\u8fdb\u4e00\u6b65\u7ec6\u5206\u6210\u4e3b\u98df\u5b50\u7cfb\u7edf\uff0c\u51c9\u83dc\u5b50\u7cfb\u7edf\uff0c\u70ed\u83dc\u5b50\u7cfb\u7edf\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
#\u4e3b\u98df\u5b50\u7cfb\u7edf\uff0c\u51c9\u83dc\u5b50\u7cfb\u7edf\uff0c\u70ed\u83dc\u5b50\u7cfb\u7edf,\u540e\u53f0\u4e09\u4e2a\u5b50\u7cfb\u7edf
<\/span> class <\/span>backSys():
    def <\/span>cook(self,dish):
        pass
class <\/span>mainFoodSys(backSys):
    def <\/span>cook(self,dish):
        print(\"MAINFOOD:Cook %s\"<\/span>%dish)
class <\/span>coolDishSys(backSys):
    def <\/span>cook(self,dish):
        print(\"COOLDISH:Cook %s\"<\/span>%dish)
class <\/span>hotDishSys(backSys):
    def <\/span>cook(self,dish):
        print(\"HOTDISH:Cook %s\"<\/span>%dish)
#\u524d\u53f0\u670d\u52a1\u5458\u7cfb\u7edf\u4e0e\u540e\u53f0\u7cfb\u7edf\u7684\u4ea4\u4e92\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u7684\u6a21\u5f0f\u6765\u5b9e\u73b0\uff0c
<\/span> #\u670d\u52a1\u5458\u5c06\u987e\u5ba2\u7684\u70b9\u5355\u5185\u5bb9\u5c01\u88c5\u6210\u547d\u4ee4\uff0c\u76f4\u63a5\u5bf9\u540e\u53f0\u4e0b\u8fbe\u547d\u4ee4\uff0c\u540e\u53f0\u5b8c\u6210\u547d\u4ee4\u8981\u6c42\u7684\u4e8b\uff0c
<\/span> #\u524d\u53f0\u7cfb\u7edf\u6784\u5efa\u5982\u4e0b
<\/span> class <\/span>waiterSys():
    menu_map=dict()
    commandList=[]
    def <\/span>setOrder(self,command):
        print(\"WAITER:Add dish\"<\/span>)
        self.commandList.append<\/span>(command)
    def <\/span>cancelOrder(self,command):
        print(\"WAITER:Cancel order...\"<\/span>)
        self.commandList.remove<\/span>(command)
    def <\/span>notify(self):
        print(\"WAITER:Notify...\"<\/span>)
        for <\/span>command in <\/span>self.commandList:
            command.execute<\/span>()
#\u524d\u53f0\u7cfb\u7edf\u4e2d\u7684notify\u63a5\u53e3\u76f4\u63a5\u8c03\u7528\u547d\u4ee4\u4e2d\u7684execute\u63a5\u53e3\uff0c\u6267\u884c\u547d\u4ee4\u3002\u547d\u4ee4\u7c7b\u6784\u5efa
<\/span> class <\/span>Command():
    receiver = None
    def <\/span>__init__(self, receiver):
        self.receiver = receiver
    def <\/span>execute(self):
        pass
class <\/span>foodCommand(Command):
    dish=\"\"<\/span><\/span><\/span>
    def <\/span>__init__(self,receiver,dish):
        self.receiver=receiver
        self.dish=dish
    def <\/span>execute(self):
        self.receiver.cook<\/span>(self.dish)
class <\/span>mainFoodCommand(foodCommand):
    pass
class <\/span>coolDishCommand(foodCommand):
    pass
class <\/span>hotDishCommand(foodCommand):
    pass
\"\"\"
Command\u7c7b\u662f\u4e2a\u6bd4\u8f83\u901a\u8fc7\u7684\u7c7b\uff0cfoodCommand\u7c7b\u662f\u672c\u4f8b\u4e2d\u6d89\u53ca\u7684\u7c7b\uff0c\u76f8\u6bd4\u4e8eCommand\u7c7b\u8fdb\u884c\u4e86\u4e00\u5b9a\u7684\u6539\u9020\u3002
\u7531\u4e8e\u540e\u53f0\u7cfb\u7edf\u4e2d\u7684\u6267\u884c\u51fd\u6570\u90fd\u662fcook\uff0c\u56e0\u800c\u5728foodCommand\u7c7b\u4e2d\u76f4\u63a5\u5c06execute\u63a5\u53e3\u5b9e\u73b0\uff0c
\u5982\u679c\u540e\u53f0\u7cfb\u7edf\u6267\u884c\u51fd\u6570\u4e0d\u540c\uff0c\u9700\u8981\u5728\u4e09\u4e2a\u5b50\u547d\u4ee4\u7cfb\u7edf\u4e2d\u5b9e\u73b0execute\u63a5\u53e3\u3002
\u8fd9\u6837\uff0c\u540e\u53f0\u4e09\u4e2a\u547d\u4ee4\u7c7b\u5c31\u53ef\u4ee5\u76f4\u63a5\u7ee7\u627f\uff0c\u4e0d\u7528\u8fdb\u884c\u4fee\u6539\u4e86\u3002
\"\"\"
#\u83dc\u5355\u7c7b\u8f85\u52a9\u4e1a\u52a1
<\/span> class <\/span>menuAll:
    menu_map=dict()
    def <\/span>loadMenu(self):#\u52a0\u8f7d\u83dc\u5355\uff0c\u8fd9\u91cc\u76f4\u63a5\u5199\u6b7b
<\/span>         self.menu_map[\"hot\"<\/span><\/span>] = [\"Yu-Shiang Shredded Pork\"<\/span><\/span>, \"Sauteed Tofu, Home Style\"<\/span><\/span>, \"Sauteed Snow Peas\"<\/span>]
        self.menu_map[\"cool\"<\/span><\/span>] = [\"Cucumber\"<\/span><\/span>, \"Preserved egg\"<\/span>]
        self.menu_map[\"main\"<\/span><\/span>] = [\"Rice\"<\/span><\/span>, \"Pie\"<\/span>]
    def <\/span>isHot(self,dish):
        if <\/span>dish in <\/span>self.menu_map[\"hot\"]:
            return True<\/span>
        return False<\/span>
    def <\/span>isCool(self,dish):
        if <\/span>dish in <\/span>self.menu_map[\"cool\"]:
            return True<\/span>
        return False<\/span>
    def <\/span>isMain(self,dish):
        if <\/span>dish in <\/span>self.menu_map[\"main\"]:
            return True<\/span>
        return False<\/span>
#\u4e1a\u52a1\u573a\u666f
<\/span> if <\/span> __name__==\"__main__\"<\/span>:
    dish_list=[\"Yu-Shiang Shredded Pork\",\"Sauteed Tofu, Home Style\",\"Cucumber\",\"Rice\"]#\u987e\u5ba2\u70b9\u7684\u83dc
<\/span>     waiter_sys=waiterSys()
    main_food_sys=mainFoodSys()
    cool_dish_sys=coolDishSys()
    hot_dish_sys=hotDishSys()
    menu=menuAll()
    menu.loadMenu<\/span>()
    for <\/span>dish in <\/span>dish_list:
        if <\/span>menu.isCool<\/span>(dish):
            cmd=coolDishCommand(cool_dish_sys,dish)
        elif <\/span><\/span>menu.isHot<\/span>(dish):
            cmd=hotDishCommand(hot_dish_sys,dish)
        elif <\/span><\/span>menu.isMain<\/span>(dish):
            cmd=mainFoodCommand(main_food_sys,dish)
        else:<\/span>
            continue<\/span>
        waiter_sys.setOrder<\/span>(cmd)
    waiter_sys.notify<\/span>()
<\/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
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
WAITER:Add dish
WAITER:Add dish
WAITER:Add dish
WAITER:Add dish
WAITER:Notify...
HOTDISH:Cook Yu-Shiang Shredded Pork
HOTDISH:Cook Sauteed Tofu, Home Style
COOLDISH:Cook Cucumber
MAINFOOD:Cook Rice
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8bf4\u660e<\/h3>\n
\n \u8f93\u51fa\u5b9e\u73b0\u4ee5Python\u8bed\u8a00\u5217\u51fa\u7684\u6240\u6709\u547d\u4ee4\u548c\u5173\u952e\u5b57\u3002\u5b83\u4f1a\u6253\u5370\u51fa\u5fc5\u8981\u7684\u53d8\u91cf\u503c\u3002\n <\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u547d\u4ee4\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-2111","post","type-post","status-publish","format-standard","hentry","category-python-sjms"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/2111"}],"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=2111"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/2111\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=2111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=2111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=2111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}