{"id":2008,"date":"2024-05-02T11:56:10","date_gmt":"2024-05-02T03:56:10","guid":{"rendered":"https:\/\/wp.minicoda.com\/?p=2008"},"modified":"2024-11-19T15:41:24","modified_gmt":"2024-11-19T07:41:24","slug":"case%e8%af%ad%e5%8f%a5%e5%8f%af%e4%bb%a5%e5%8a%a8%e6%80%81%e7%ad%9b%e9%80%89","status":"publish","type":"post","link":"https:\/\/wp.minicoda.com\/index.php\/case%e8%af%ad%e5%8f%a5%e5%8f%af%e4%bb%a5%e5%8a%a8%e6%80%81%e7%ad%9b%e9%80%89\/","title":{"rendered":"case\u8bed\u53e5\u53ef\u4ee5\u52a8\u6001\u7b5b\u9009?"},"content":{"rendered":"\n<p>  \u4f20\u7edf\u610f\u4e49\u4e0a\uff0cC\/ObjC\/C++\u4e0d\u652f\u6301case\u5339\u914d\u7684\u52a8\u6001\u7b5b\u9009\uff0c\u53ea\u6709\u9759\u6001\u4e14\u5177\u4f53\u503c\u5bf9\u5e94\u5339\u914d\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Swift\u63d0\u4f9b\u4e86case <strong><em>&lt;pattern&gt;<\/em><\/strong> \u53ef\u4ee5\u52a8\u6001\u7b5b\u9009\u6761\u4ef6\uff0c\u6bd4\u8f83\u7075\u6d3b\u5f3a\u5927\u3002\u6280\u672f\u4e0a\u5e76\u4e0d\u590d\u6742\uff0c\u53ea\u662f\u5c06\u4e4b\u524d\u7684\u5355\u4e00\u9759\u6001\u6761\u4ef6\u5224\u65ad\u53d8\u6210<strong><em>&lt;pattern&gt;<\/em><\/strong>\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u3002<\/li>\n\n\n\n<li>C++17\u5141\u8bb8switch\u521d\u59cb\u5316\u8868\u8fbe\u5f0f(<a href=\"https:\/\/learn.microsoft.com\/zh-cn\/cpp\/cpp\/switch-statement-cpp?view=msvc-170#syntax\">switch \u8bed\u53e5 (C++) | Microsoft Learn<\/a>)\uff0c\u4f46case\u4f9d\u7136\u5c40\u9650\u4e8e\u5177\u4f53\u503c\u8303\u56f4\uff0c\u4e0d\u80fd\u52a0\u5165\u53d8\u91cf\u6761\u4ef6\u3002<br>GNU\u6269\u5c55\u5bf9C\/C++\u90fd\u5141\u8bb8case\u8303\u56f4\u5339\u914d\uff0c\u4f8b\u5982\u5982\u4e0bcase 1 ... 10\uff0c\u4f46MSVC\u4e0d\u652f\u6301\u3002<br>  <strong><em>switch (int x = value; x) {  <\/em><\/strong><em>\/\/ Only C++17 works<\/em><strong><em><br>  case 1 ... 10:<\/em><\/strong>    <em>\/\/ GNU C\/C++ both works<br>  <\/em><strong><em>}<\/em><\/strong><br>  - \u6b64\u5904\u9700\u7279\u522b\u6ce8\u610f\uff0ccase 1...10\u4f1a\u7f16\u8bd1\u51fa\u9519\uff0c\"...\"\u524d\u540e\u5fc5\u987b\u6709\u7a7a\u683c<a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Case-Ranges.html\">(Case Ranges | GCC<\/a>).<br>  - \u53ef\u80fd\u4f1a\u53d1\u73b0\u7528gcc -std=c89\u6216g++ -std=c++98\u90fd\u53ef\u4ee5\u7f16\u8bd1\u8fc7\u5982\u4e0acase 1 ... 10\uff0c\u4e0d\u8981\u60ca\u614c\uff0c\u53ef\u80fd\u4f60\u4f7f\u7528\u7684GCC\u7248\u672cdefault\u5c31\u542f\u7528case\u8303\u56f4\u5339\u914d\uff0c\u54ea\u6015\u6307\u5b9a\u7528\u6807\u51c6C\u8bed\u8a00\u6216\u8005C++\u4e5f\u4e0d\u80fd\u963b\u6321GCC\u5bf9\u8303\u56f4\u5339\u914d\u7684\u652f\u6301\u3002<\/li>\n\n\n\n<li>C# 7.0 \u5f15\u5165\u7684\u6a21\u5f0f\u5339\u914d\uff08Pattern Matching\uff09\u5141\u8bb8\u5f00\u53d1\u8005\u5728&nbsp;<code>switch<\/code>&nbsp;\u8bed\u53e5\u4e2d\u4f7f\u7528\u66f4\u590d\u6742\u7684\u6761\u4ef6\u6765\u5339\u914d\u4e0d\u540c\u7684\u60c5\u51b5\uff0c\u4e0d\u6b62\u57fa\u4e8e\u5e38\u91cf\u503c\uff0c\u8fd8\u80fd\u57fa\u4e8e\u6570\u636e\u7c7b\u578b\u3001\u5c5e\u6027\u3001\u6761\u4ef6\u5173\u7cfb\u7b49\u8fdb\u884c\u5206\u652f\uff0c\u56e0\u4e3a\u592a\u7075\u6d3b\u4e86\uff0c\u6b64\u5904\u4e0d\u4e3e\u4f8b\u4e86\u3002<\/li>\n\n\n\n<li>Java 12\u5bf9switch\u8868\u8fbe\u5f0f\u8fdb\u884c\u4f18\u5316\uff0c\u5141\u8bb8\u591a\u503c\u5e76\u5e26\u6709\u6761\u4ef6\u5224\u65ad\u3002<\/li>\n\n\n\n<li>JavaScript case\u5141\u8bb8\u4efb\u610f\u8868\u8fbe\u5f0f\uff0c\u4e14case\u8868\u8fbe\u5f0f\u662f\u8fd0\u884c\u65f6\u6c42\u503c\u800c\u975eC\/C++\u7f16\u8bd1\u671f\u6c42\u503c\uff0c\u6240\u4ee5JS\u7684case\u8868\u8fbe\u5f0f\u6027\u80fd\u4f1a\u4f4e\u5f88\u591a\u3002<\/li>\n\n\n\n<li>Kotlin \u7528when\u8bed\u53e5\u652f\u6301\u4f20\u7edf\u8bed\u8a00\u7684switch\/case\uff0c\u4e5f\u652f\u6301\u8303\u56f4\u5339\u914d\u548c\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u6761\u4ef6\u5224\u65ad\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4f20\u7edf\u610f\u4e49\u4e0a\uff0cC\/ObjC\/C++\u4e0d\u652f\u6301case\u5339\u914d\u7684\u52a8\u6001\u7b5b\u9009\uff0c\u53ea\u6709\u9759\u6001\u4e14\u5177\u4f53\u503c\u5bf9\u5e94\u5339\u914d\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[299],"tags":[],"class_list":["post-2008","post","type-post","status-publish","format-standard","hentry","category-think"],"views":63,"_links":{"self":[{"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/posts\/2008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/comments?post=2008"}],"version-history":[{"count":6,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/posts\/2008\/revisions"}],"predecessor-version":[{"id":3420,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/posts\/2008\/revisions\/3420"}],"wp:attachment":[{"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/media?parent=2008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/categories?post=2008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/tags?post=2008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}