{"id":63009,"date":"2026-01-21T07:41:24","date_gmt":"2026-01-20T23:41:24","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/63009.html"},"modified":"2026-01-21T07:41:24","modified_gmt":"2026-01-20T23:41:24","slug":"%e3%80%90c-%e8%bf%9b%e9%98%b6%e3%80%91%e6%8b%92%e7%bb%9d%e6%95%b0%e5%8d%83%e8%a1%8c-switch-case%ef%bc%9a%e5%9c%a8%e5%b5%8c%e5%85%a5%e5%bc%8f%e7%b3%bb%e7%bb%9f%e4%b8%ad%e4%bc%98%e9%9b%85%e5%ae%9e","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/63009.html","title":{"rendered":"\u3010C++ \u8fdb\u9636\u3011\u62d2\u7edd\u6570\u5343\u884c switch-case\uff1a\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u4f18\u96c5\u5b9e\u73b0\u201c\u72b6\u6001\u6a21\u5f0f\u201d (State Pattern)"},"content":{"rendered":"<p>\u6458\u8981&#xff1a;\u5728\u5d4c\u5165\u5f0f\u5f00\u53d1\u4e2d&#xff0c;\u6211\u4eec\u4e60\u60ef\u7528 switch-case \u6765\u5b9e\u73b0\u6709\u9650\u72b6\u6001\u673a&#xff08;FSM&#xff09;\u3002\u4f46\u968f\u7740\u4e1a\u52a1\u903b\u8f91\u53d8\u590d\u6742&#xff0c;\u72b6\u6001\u4ece 3 \u4e2a\u53d8\u6210 20 \u4e2a&#xff0c;case \u5206\u652f\u91cc\u7684\u4ee3\u7801\u4f1a\u81a8\u80c0\u6210\u96be\u4ee5\u7ef4\u62a4\u7684\u201c\u9762\u6761\u4ee3\u7801\u201d\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528 C&#043;&#043; \u7684\u591a\u6001&#xff08;Polymorphism&#xff09;\u548c\u72b6\u6001\u6a21\u5f0f&#xff08;State Pattern&#xff09;&#xff0c;\u5c06\u6bcf\u4e2a\u72b6\u6001\u5c01\u88c5\u4e3a\u72ec\u7acb\u7684\u7c7b&#xff0c;\u5b9e\u73b0\u903b\u8f91\u7684\u5f7b\u5e95\u89e3\u8026\u4e0e\u96f6\u6210\u672c\u6269\u5c55\u3002<\/p>\n<h3>\u4e00\u3001 \u5669\u68a6\u7684\u5f00\u59cb&#xff1a;\u4f20\u7edf\u7684 Switch-Case \u72b6\u6001\u673a<\/h3>\n<p>\u5047\u8bbe\u4f60\u6b63\u5728\u505a\u4e00\u4e2a\u626b\u5730\u673a\u5668\u4eba\u3002\u6700\u521d\u53ea\u6709\u4e09\u4e2a\u72b6\u6001&#xff1a;\u5f85\u673a\u3001\u6e05\u626b\u3001\u5145\u7535\u3002 C \u8bed\u8a00\u7684\u5199\u6cd5\u901a\u5e38\u662f\u8fd9\u6837\u7684&#xff1a;<\/p>\n<p>typedef enum { STATE_IDLE, STATE_CLEAN, STATE_CHARGE, STATE_ERROR } RobotState;<\/p>\n<p>void Robot_Run() {<br \/>\n    static RobotState currentState &#061; STATE_IDLE;<\/p>\n<p>    switch (currentState) {<br \/>\n        case STATE_IDLE:<br \/>\n            if (Battery &lt; 20) currentState &#061; STATE_CHARGE;<br \/>\n            if (UserPressStart) currentState &#061; STATE_CLEAN;<br \/>\n            \/\/ &#8230; \u6b64\u5904\u7701\u7565 50 \u884c LED \u63a7\u5236\u4ee3\u7801 &#8230;<br \/>\n            break;<\/p>\n<p>        case STATE_CLEAN:<br \/>\n            Motor_Start();<br \/>\n            if (ObstacleDetected) { \/* \u907f\u969c\u903b\u8f91 100 \u884c *\/ }<br \/>\n            if (Battery &lt; 10) currentState &#061; STATE_CHARGE;<br \/>\n            break;<\/p>\n<p>        case STATE_CHARGE:<br \/>\n            \/\/ &#8230; \u5145\u7535\u903b\u8f91 &#8230;<br \/>\n            break;<br \/>\n    }<br \/>\n}<\/p>\n<h4>\u8fd9\u79cd\u5199\u6cd5\u7684\u75db\u70b9&#xff1a;<\/h4>\n<li>\n<p>\u6587\u4ef6\u81c3\u80bf&#xff1a;\u6240\u6709\u7684\u903b\u8f91\u90fd\u585e\u5728 main.c \u6216 robot.c \u7684\u4e00\u4e2a\u51fd\u6570\u91cc&#xff0c;\u8fd9\u4e2a\u51fd\u6570\u5f88\u5feb\u5c31\u4f1a\u8d85\u8fc7 1000 \u884c\u3002<\/p>\n<\/li>\n<li>\n<p>\u53d8\u91cf\u6c61\u67d3&#xff1a;\u4e3a\u4e86\u5728\u4e0d\u540c\u72b6\u6001\u95f4\u5171\u4eab\u6570\u636e&#xff0c;\u4f60\u4e0d\u5f97\u4e0d\u5b9a\u4e49\u5927\u91cf\u7684\u5168\u5c40\u53d8\u91cf\u6216\u9759\u6001\u53d8\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p>\u6269\u5c55\u5730\u72f1&#xff1a;\u73b0\u5728\u8001\u677f\u8ba9\u4f60\u52a0\u4e00\u4e2a\u201c\u624b\u52a8\u9065\u63a7\u6a21\u5f0f\u201d\u3002\u4f60\u5fc5\u987b\u53bb\u6539\u90a3\u4e2a\u5de8\u5927\u7684 switch&#xff0c;\u5f88\u5bb9\u6613\u4e0d\u5c0f\u5fc3\u628a\u201c\u81ea\u52a8\u6e05\u626b\u201d\u7684\u903b\u8f91\u641e\u574f\u3002<\/p>\n<\/li>\n<hr \/>\n<h3>\u4e8c\u3001 \u7834\u5c40&#xff1a;\u9762\u5411\u5bf9\u8c61\u7684\u72b6\u6001\u6a21\u5f0f<\/h3>\n<p>\u6211\u4eec\u8981\u8f6c\u6362\u601d\u7ef4&#xff1a;\u72b6\u6001\u4e0d\u662f\u4e00\u4e2a\u679a\u4e3e\u503c&#xff0c;\u72b6\u6001\u5e94\u8be5\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<ul>\n<li>\n<p>\u5f85\u673a\u662f\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5b83\u53ea\u7ba1\u5f85\u673a\u8be5\u5e72\u7684\u4e8b\u3002<\/p>\n<\/li>\n<li>\n<p>\u6e05\u626b\u662f\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5b83\u53ea\u7ba1\u6e05\u626b\u8be5\u5e72\u7684\u4e8b\u3002<\/p>\n<\/li>\n<li>\n<p>\u673a\u5668\u4eba&#xff08;Context&#xff09;\u662f\u4e00\u4e2a\u5bb9\u5668&#xff0c;\u5b83\u624b\u91cc\u62ff\u7740\u5f53\u524d\u7684\u72b6\u6001\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>1. \u5b9a\u4e49\u72b6\u6001\u57fa\u7c7b (The Interface)<\/h4>\n<p>\u8fd9\u662f\u6240\u6709\u72b6\u6001\u5fc5\u987b\u9075\u5b88\u7684\u5951\u7ea6\u3002\u6ce8\u610f\u6211\u4eec\u5b9a\u4e49\u4e86 Enter&#xff08;\u8fdb\u5165&#xff09;\u3001Run&#xff08;\u8fd0\u884c&#xff09;\u3001Exit&#xff08;\u9000\u51fa&#xff09;\u4e09\u4e2a\u6807\u51c6\u751f\u547d\u5468\u671f\u3002<\/p>\n<p>\/\/ RobotState.h<br \/>\nclass RobotContext; \/\/ \u524d\u7f6e\u58f0\u660e<\/p>\n<p>class RobotState {<br \/>\npublic:<br \/>\n    virtual ~RobotState() {}<\/p>\n<p>    \/\/ \u8fdb\u5165\u8be5\u72b6\u6001\u65f6\u89e6\u53d1&#xff08;\u6bd4\u5982&#xff1a;\u5f00\u7535\u673a\u3001\u4eae\u7eff\u706f&#xff09;<br \/>\n    virtual void Enter(RobotContext* ctx) &#061; 0;<\/p>\n<p>    \/\/ \u72b6\u6001\u7684\u4e3b\u5faa\u73af\u903b\u8f91&#xff08;\u6bd4\u5982&#xff1a;PID\u8ba1\u7b97\u3001\u4f20\u611f\u5668\u68c0\u6d4b&#xff09;<br \/>\n    virtual void Run(RobotContext* ctx) &#061; 0;<\/p>\n<p>    \/\/ \u9000\u51fa\u8be5\u72b6\u6001\u65f6\u89e6\u53d1&#xff08;\u6bd4\u5982&#xff1a;\u5173\u7535\u673a\u3001\u4fdd\u5b58\u6570\u636e&#xff09;<br \/>\n    virtual void Exit(RobotContext* ctx) &#061; 0;<br \/>\n};<\/p>\n<h4>2. \u5b9a\u4e49\u673a\u5668\u4eba\u4e0a\u4e0b\u6587 (The Context)<\/h4>\n<p>\u8fd9\u4e2a\u7c7b\u8d1f\u8d23\u7ba1\u7406\u5f53\u524d\u662f\u8c01\u5728\u201c\u5f53\u5bb6\u201d\u3002<\/p>\n<p>\/\/ RobotContext.h<br \/>\n#include &#034;RobotState.h&#034;<\/p>\n<p>class RobotContext {<br \/>\nprivate:<br \/>\n    RobotState* currentState; \/\/ \u5f53\u524d\u72b6\u6001\u6307\u9488&#xff08;\u6838\u5fc3&#xff09;<\/p>\n<p>public:<br \/>\n    RobotContext() : currentState(nullptr) {}<\/p>\n<p>    \/\/ \u5207\u6362\u72b6\u6001\u7684\u9b54\u6cd5\u51fd\u6570<br \/>\n    void ChangeState(RobotState* newState) {<br \/>\n        \/\/ 1. \u9000\u51fa\u65e7\u72b6\u6001<br \/>\n        if (currentState) {<br \/>\n            currentState-&gt;Exit(this);<br \/>\n        }<br \/>\n        \/\/ 2. \u5207\u6362\u6307\u9488<br \/>\n        currentState &#061; newState;<br \/>\n        \/\/ 3. \u8fdb\u5165\u65b0\u72b6\u6001<br \/>\n        if (currentState) {<br \/>\n            currentState-&gt;Enter(this);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u4e3b\u5faa\u73af\u8c03\u7528\u5b83<br \/>\n    void Loop() {<br \/>\n        if (currentState) {<br \/>\n            currentState-&gt;Run(this);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u786c\u4ef6\u63a5\u53e3&#xff08;\u4f9b\u72b6\u6001\u7c7b\u8c03\u7528&#xff09;<br \/>\n    void SetMotor(int speed) { \/*&#8230;*\/ }<br \/>\n    int GetBattery() { return 80; }<br \/>\n};<\/p>\n<h3>\u4e09\u3001 \u5b9e\u73b0\u5177\u4f53\u72b6\u6001&#xff1a;\u89e3\u8026\u7684\u827a\u672f<\/h3>\n<p>\u73b0\u5728&#xff0c;\u6211\u4eec\u53ef\u4ee5\u628a\u6bcf\u4e2a\u72b6\u6001\u5199\u5728\u72ec\u7acb\u7684\u6587\u4ef6\u91cc\u3002<\/p>\n<h4>1. \u5f85\u673a\u72b6\u6001 (IdleState)<\/h4>\n<p>\/\/ IdleState.cpp<br \/>\n#include &#034;IdleState.h&#034;<br \/>\n#include &#034;CleaningState.h&#034; \/\/ \u9700\u8981\u5f15\u7528\u4e0b\u4e00\u4e2a\u53ef\u80fd\u8df3\u8f6c\u7684\u72b6\u6001<\/p>\n<p>\/\/ \u4e3a\u4e86\u907f\u514d\u9891\u7e41 new\/delete&#xff0c;\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f&#xff08;\u9759\u6001\u5c40\u90e8\u53d8\u91cf&#xff09;<br \/>\nRobotState* IdleState::Instance() {<br \/>\n    static IdleState instance;<br \/>\n    return &amp;instance;<br \/>\n}<\/p>\n<p>void IdleState::Enter(RobotContext* ctx) {<br \/>\n    printf(&#034;[Idle] Stop Motors, LED Green.\\\\n&#034;);<br \/>\n    ctx-&gt;SetMotor(0);<br \/>\n}<\/p>\n<p>void IdleState::Run(RobotContext* ctx) {<br \/>\n    \/\/ \u53ea\u9700\u8981\u5173\u6ce8\u5f85\u673a\u65f6\u7684\u903b\u8f91<br \/>\n    if (ctx-&gt;GetBattery() &lt; 10) {<br \/>\n        \/\/ \u5207\u6362\u5230\u5145\u7535\u72b6\u6001<br \/>\n        \/\/ ctx-&gt;ChangeState(ChargingState::Instance());<br \/>\n    }<\/p>\n<p>    if (User_Is_Pressed()) {<br \/>\n        \/\/ \u5207\u6362\u5230\u6e05\u626b\u72b6\u6001<br \/>\n        ctx-&gt;ChangeState(CleaningState::Instance());<br \/>\n    }<br \/>\n}<\/p>\n<p>void IdleState::Exit(RobotContext* ctx) {<br \/>\n    printf(&#034;[Idle] Preparing to work&#8230;\\\\n&#034;);<br \/>\n}<\/p>\n<p>2. \u6e05\u626b\u72b6\u6001 (CleaningState)<\/p>\n<p>\/\/ CleaningState.cpp<br \/>\nvoid CleaningState::Enter(RobotContext* ctx) {<br \/>\n    printf(&#034;[Clean] Motor ON, LED Blue.\\\\n&#034;);<br \/>\n    ctx-&gt;SetMotor(100);<br \/>\n}<\/p>\n<p>void CleaningState::Run(RobotContext* ctx) {<br \/>\n    \/\/ \u53ea\u5199\u6e05\u626b\u903b\u8f91&#xff0c;\u5b8c\u5168\u4e0d\u7528\u7ba1\u600e\u4e48\u5f85\u673a<br \/>\n    DoPathPlanning();<\/p>\n<p>    if (Error_Occurred()) {<br \/>\n        \/\/ \u53d1\u751f\u6545\u969c&#xff0c;\u5207\u6362\u6545\u969c\u72b6\u6001<br \/>\n        \/\/ ctx-&gt;ChangeState(ErrorState::Instance());<br \/>\n    }<br \/>\n}<\/p>\n<p>void CleaningState::Exit(RobotContext* ctx) {<br \/>\n    ctx-&gt;SetMotor(0); \/\/ \u9000\u51fa\u6e05\u626b\u5fc5\u987b\u5173\u7535\u673a&#xff0c;\u9632\u6b62\u66b4\u8d70<br \/>\n}<\/p>\n<h3>\u56db\u3001 \u8fd9\u79cd\u67b6\u6784\u597d\u5728\u54ea\u91cc&#xff1f;<\/h3>\n<h4>1. \u5f7b\u5e95\u6d88\u706d\u4e86 if-else \/ switch<\/h4>\n<p>\u5728 RobotContext::Loop() \u4e2d&#xff0c;\u4f60\u53ea\u770b\u5230\u4e00\u884c\u4ee3\u7801&#xff1a;<\/p>\n<p>currentState-&gt;Run(this);<\/p>\n<p>\u7cfb\u7edf\u5b8c\u5168\u4e0d\u9700\u8981\u77e5\u9053\u5f53\u524d\u662f Idle \u8fd8\u662f Cleaning&#xff0c;\u5b83\u5229\u7528 C&#043;&#043; \u7684\u591a\u6001\u6027&#xff08;Virtual Function&#xff09; \u81ea\u52a8\u8df3\u8f6c\u5230\u4e86\u5bf9\u5e94\u7684\u4ee3\u7801\u6bb5\u3002\u8fd9\u53eb\u201c\u52a8\u6001\u7ed1\u5b9a\u201d\u3002<\/p>\n<h4>2. \u65b0\u589e\u72b6\u6001 &#061; \u65b0\u589e\u6587\u4ef6<\/h4>\n<p>\u5982\u679c\u8001\u677f\u8981\u52a0\u201c\u9065\u63a7\u6a21\u5f0f\u201d&#xff1a;<\/p>\n<li>\n<p>\u65b0\u5efa RemoteState.cpp\u3002<\/p>\n<\/li>\n<li>\n<p>\u7ee7\u627f RobotState&#xff0c;\u5b9e\u73b0 Enter\/Run\/Exit\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728 Idle \u72b6\u6001\u91cc\u52a0\u4e00\u4e2a\u5224\u65ad\u8df3\u8f6c\u8fc7\u53bb\u3002 \u539f\u6709\u7684 CleaningState\u3001ChargingState \u4ee3\u7801\u4e00\u884c\u90fd\u4e0d\u7528\u52a8&#xff01; \u8fd9\u6781\u5927\u5730\u964d\u4f4e\u4e86\u5f15\u5165 Bug \u7684\u98ce\u9669\u3002<\/p>\n<\/li>\n<h4>3. \u539f\u5b50\u5316\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406<\/h4>\n<p>\u4ee5\u524d\u5199 Switch \u65f6&#xff0c;\u4ece\u201c\u6e05\u626b\u201d\u8df3\u5230\u201c\u5f85\u673a\u201d&#xff0c;\u5f88\u5bb9\u6613\u5fd8\u8bb0\u5173\u7535\u673a\u3002 \u73b0\u5728&#xff0c;ChangeState \u51fd\u6570\u5f3a\u5236\u6267\u884c oldState-&gt;Exit()\u3002\u4f60\u53ea\u9700\u8981\u5728 CleaningState::Exit \u91cc\u5199\u4e0a\u5173\u7535\u673a&#xff0c;\u65e0\u8bba\u4f60\u662f\u56e0\u4e3a\u6ca1\u7535\u4e86\u9000\u51fa&#xff0c;\u8fd8\u662f\u56e0\u4e3a\u7528\u6237\u6309\u505c\u4e86\u9000\u51fa&#xff0c;\u7535\u673a\u4e00\u5b9a\u4f1a\u88ab\u5173\u95ed\u3002<\/p>\n<hr \/>\n<h3>\u4e94\u3001 \u5d4c\u5165\u5f0f\u7279\u4f9b\u4f18\u5316&#xff1a;\u5185\u5b58\u788e\u7247\u95ee\u9898<\/h3>\n<p>\u6807\u51c6\u7684 Design Pattern \u7ecf\u5e38\u5728\u5207\u6362\u72b6\u6001\u65f6\u4f7f\u7528 new CleaningState()&#xff0c;\u65e7\u72b6\u6001 delete this\u3002 \u4f46\u5728 STM32 \u4e0a&#xff0c;\u9891\u7e41\u7684\u5806\u5185\u5b58\u5206\u914d\u4f1a\u5bfc\u81f4\u788e\u7247\u5316\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u5355\u4f8b\u72b6\u6001 (Singleton States) \u5982\u4ee3\u7801\u6240\u793a&#xff0c;\u6bcf\u4e2a\u72b6\u6001\u7c7b\u5185\u90e8\u7ef4\u62a4\u4e00\u4e2a static \u5b9e\u4f8b&#xff1a;<\/p>\n<p>static IdleState instance;<br \/>\nreturn &amp;instance;<\/p>\n<p>\u8fd9\u6837\u6240\u6709\u72b6\u6001\u5bf9\u8c61\u90fd\u5728\u9759\u6001\u5b58\u50a8\u533a&#xff08;BSS\/Data\u6bb5&#xff09;&#xff0c;\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u5185\u5b58\u5360\u7528\u662f\u56fa\u5b9a\u7684&#xff0c;\u96f6 malloc&#xff0c;\u96f6\u98ce\u9669\u3002<\/p>\n<hr \/>\n<h3>\u516d\u3001 \u603b\u7ed3<\/h3>\n<p>\u5f88\u591a\u5d4c\u5165\u5f0f\u5de5\u7a0b\u5e08\u8ba4\u4e3a C&#043;&#043; \u81c3\u80bf&#xff0c;\u662f\u56e0\u4e3a\u4ed6\u4eec\u8fd8\u5728\u7528 C \u7684\u601d\u7ef4\u5199 C&#043;&#043;&#xff08;Class \u53ea\u662f struct \u7684\u522b\u540d&#xff09;\u3002<\/p>\n<p>\u771f\u6b63\u7684 C&#043;&#043; \u5a01\u529b\u5728\u4e8e\u67b6\u6784\u8bbe\u8ba1\u3002\u901a\u8fc7\u72b6\u6001\u6a21\u5f0f&#xff0c;\u6211\u4eec\u5c06\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\u62c6\u89e3\u6210\u4e86\u72ec\u7acb\u7684\u3001\u53ef\u6d4b\u8bd5\u7684\u5fae\u5c0f\u5355\u5143\u3002<\/p>\n<ul>\n<li>\n<p>Switch-Case \u9002\u5408\u7b80\u5355\u7684\u3001\u72b6\u6001\u5c11\u4e8e 3 \u4e2a\u7684\u903b\u8f91\u3002<\/p>\n<\/li>\n<li>\n<p>\u72b6\u6001\u6a21\u5f0f \u662f\u4e2d\u5927\u578b\u9879\u76ee&#xff08;\u673a\u5668\u4eba\u3001\u98de\u884c\u5668\u3001\u590d\u6742\u5de5\u63a7&#xff09;\u7684\u6807\u914d\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e0b\u4e00\u6b21&#xff0c;\u5f53\u4f60\u7684 switch \u5199\u5230\u7b2c 500 \u884c\u7684\u65f6\u5019&#xff0c;\u8bb0\u5f97\u56de\u6765\u770b\u770b\u8fd9\u7bc7\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981&#xff1a;\u5728\u5d4c\u5165\u5f0f\u5f00\u53d1\u4e2d&#xff0c;\u6211\u4eec\u4e60\u60ef\u7528 switch-case \u6765\u5b9e\u73b0\u6709\u9650\u72b6\u6001\u673a&#xff08;FSM&#xff09;\u3002\u4f46\u968f\u7740\u4e1a\u52a1\u903b\u8f91\u53d8\u590d\u6742&#xff0c;\u72b6\u6001\u4ece 3 \u4e2a\u53d8\u6210 20 \u4e2a&#xff0c;case \u5206\u652f\u91cc\u7684\u4ee3\u7801\u4f1a\u81a8\u80c0\u6210\u96be\u4ee5\u7ef4\u62a4\u7684\u201c\u9762\u6761\u4ee3\u7801\u201d\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528 C \u7684\u591a\u6001&#xff08;Polymorphism&#xff09;\u548c\u72b6\u6001\u6a21\u5f0f&#xff08;State Pattern&#xff09;&#xff0c;\u5c06\u6bcf\u4e2a\u72b6\u6001\u5c01\u88c5\u4e3a\u72ec\u7acb\u7684\u7c7b&amp;#xff<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1654],"topic":[],"class_list":["post-63009","post","type-post","status-publish","format-standard","hentry","category-server","tag-1654"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010C++ \u8fdb\u9636\u3011\u62d2\u7edd\u6570\u5343\u884c switch-case\uff1a\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u4f18\u96c5\u5b9e\u73b0\u201c\u72b6\u6001\u6a21\u5f0f\u201d (State Pattern) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/63009.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010C++ \u8fdb\u9636\u3011\u62d2\u7edd\u6570\u5343\u884c switch-case\uff1a\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u4f18\u96c5\u5b9e\u73b0\u201c\u72b6\u6001\u6a21\u5f0f\u201d (State Pattern) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6458\u8981&#xff1a;\u5728\u5d4c\u5165\u5f0f\u5f00\u53d1\u4e2d&#xff0c;\u6211\u4eec\u4e60\u60ef\u7528 switch-case \u6765\u5b9e\u73b0\u6709\u9650\u72b6\u6001\u673a&#xff08;FSM&#xff09;\u3002\u4f46\u968f\u7740\u4e1a\u52a1\u903b\u8f91\u53d8\u590d\u6742&#xff0c;\u72b6\u6001\u4ece 3 \u4e2a\u53d8\u6210 20 \u4e2a&#xff0c;case \u5206\u652f\u91cc\u7684\u4ee3\u7801\u4f1a\u81a8\u80c0\u6210\u96be\u4ee5\u7ef4\u62a4\u7684\u201c\u9762\u6761\u4ee3\u7801\u201d\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528 C \u7684\u591a\u6001&#xff08;Polymorphism&#xff09;\u548c\u72b6\u6001\u6a21\u5f0f&#xff08;State Pattern&#xff09;&#xff0c;\u5c06\u6bcf\u4e2a\u72b6\u6001\u5c01\u88c5\u4e3a\u72ec\u7acb\u7684\u7c7b&amp;#xff\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/63009.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-20T23:41:24+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/63009.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/63009.html\",\"name\":\"\u3010C++ \u8fdb\u9636\u3011\u62d2\u7edd\u6570\u5343\u884c switch-case\uff1a\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u4f18\u96c5\u5b9e\u73b0\u201c\u72b6\u6001\u6a21\u5f0f\u201d (State Pattern) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-20T23:41:24+00:00\",\"dateModified\":\"2026-01-20T23:41:24+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/63009.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/63009.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/63009.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010C++ \u8fdb\u9636\u3011\u62d2\u7edd\u6570\u5343\u884c switch-case\uff1a\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u4f18\u96c5\u5b9e\u73b0\u201c\u72b6\u6001\u6a21\u5f0f\u201d (State Pattern)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010C++ \u8fdb\u9636\u3011\u62d2\u7edd\u6570\u5343\u884c switch-case\uff1a\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u4f18\u96c5\u5b9e\u73b0\u201c\u72b6\u6001\u6a21\u5f0f\u201d (State Pattern) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/63009.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010C++ \u8fdb\u9636\u3011\u62d2\u7edd\u6570\u5343\u884c switch-case\uff1a\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u4f18\u96c5\u5b9e\u73b0\u201c\u72b6\u6001\u6a21\u5f0f\u201d (State Pattern) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6458\u8981&#xff1a;\u5728\u5d4c\u5165\u5f0f\u5f00\u53d1\u4e2d&#xff0c;\u6211\u4eec\u4e60\u60ef\u7528 switch-case \u6765\u5b9e\u73b0\u6709\u9650\u72b6\u6001\u673a&#xff08;FSM&#xff09;\u3002\u4f46\u968f\u7740\u4e1a\u52a1\u903b\u8f91\u53d8\u590d\u6742&#xff0c;\u72b6\u6001\u4ece 3 \u4e2a\u53d8\u6210 20 \u4e2a&#xff0c;case \u5206\u652f\u91cc\u7684\u4ee3\u7801\u4f1a\u81a8\u80c0\u6210\u96be\u4ee5\u7ef4\u62a4\u7684\u201c\u9762\u6761\u4ee3\u7801\u201d\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528 C \u7684\u591a\u6001&#xff08;Polymorphism&#xff09;\u548c\u72b6\u6001\u6a21\u5f0f&#xff08;State Pattern&#xff09;&#xff0c;\u5c06\u6bcf\u4e2a\u72b6\u6001\u5c01\u88c5\u4e3a\u72ec\u7acb\u7684\u7c7b&amp;#xff","og_url":"https:\/\/www.wsisp.com\/helps\/63009.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-20T23:41:24+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/63009.html","url":"https:\/\/www.wsisp.com\/helps\/63009.html","name":"\u3010C++ \u8fdb\u9636\u3011\u62d2\u7edd\u6570\u5343\u884c switch-case\uff1a\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u4f18\u96c5\u5b9e\u73b0\u201c\u72b6\u6001\u6a21\u5f0f\u201d (State Pattern) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-20T23:41:24+00:00","dateModified":"2026-01-20T23:41:24+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/63009.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/63009.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/63009.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010C++ \u8fdb\u9636\u3011\u62d2\u7edd\u6570\u5343\u884c switch-case\uff1a\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u4f18\u96c5\u5b9e\u73b0\u201c\u72b6\u6001\u6a21\u5f0f\u201d (State Pattern)"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/63009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=63009"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/63009\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=63009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=63009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=63009"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=63009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}