{"id":202544,"date":"2025-05-22T09:19:57","date_gmt":"2025-05-22T01:19:57","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202544\/"},"modified":"2025-05-22T09:19:57","modified_gmt":"2025-05-22T01:19:57","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e6%8e%a5%e5%8f%a3%e5%9c%a8%e6%a8%a1%e5%bc%8f%e5%8c%b9%e9%85%8d%e5%92%8c%e7%ad%96%e7%95%a5%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e4%b8%ad%e7%9a%84%e4%bc%98%e5%8a%bf","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202544\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6a21\u5f0f\u5339\u914d\u548c\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u4f18\u52bf\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6a21\u5f0f\u5339\u914d\u548c\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u4f18\u52bf\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aJava\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6a21\u5f0f\u5339\u914d\u548c\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u4f18\u52bf\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p>Java \u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6a21\u5f0f\u5339\u914d\u4e2d\u53ef\u7b80\u5316\u6a21\u5f0f\u5339\u914d\uff0c\u5728\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u53ef\u8f7b\u677e\u5b9e\u73b0\u7b56\u7565\u3002\u6a21\u5f0f\u5339\u914d\u5141\u8bb8\u4f7f\u7528 Pattern Matching for instanceof \u6bd4\u8f83\u8f93\u5165\u503c\u5e76\u6267\u884c\u4e0d\u540c\u64cd\u4f5c\uff1b\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\u4f7f\u7528\u5bf9\u8c61\u884c\u4e3a\u89e3\u8026\uff0c\u51fd\u6570\u5f0f\u63a5\u53e3\u53ef\u8f7b\u677e\u5b9e\u73b0\u7b56\u7565\u4ea4\u6362\uff0c\u4f8b\u5982\u8ba1\u7b97\u6298\u6263\u7684\u6848\u4f8b\u4e2d\uff0c\u6839\u636e\u7528\u6237\u7c7b\u578b\u9009\u62e9\u4e0d\u540c\u6298\u6263\u7b56\u7565\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729916273671c6d711bfa6.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6a21\u5f0f\u5339\u914d\u548c\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u4f18\u52bf<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u662f\u4e00\u79cd\u53ea\u5305\u542b\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u7684\u63a5\u53e3\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u62bd\u8c61\u6982\u5ff5\u7684\u5f3a\u5927\u65b9\u5f0f\uff0c\u5728\u6a21\u5f0f\u5339\u914d\u548c\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u5c24\u5176\u6709\u7528\u3002<\/p>\n<p><strong>\u6a21\u5f0f\u5339\u914d<\/strong><\/p>\n<p>\u6a21\u5f0f\u5339\u914d\u5141\u8bb8\u5c06\u8f93\u5165\u503c\u4e0e\u4e00\u7ec4\u6a21\u5f0f\u8fdb\u884c\u6bd4\u8f83\uff0c\u5e76\u6839\u636e\u5339\u914d\u7ed3\u679c\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002Java 14 \u5f15\u5165\u4e86 <code>Pattern Matching for instanceof<\/code>\uff0c\u5141\u8bb8\u4f7f\u7528\u51fd\u6570\u5f0f\u63a5\u53e3\u7b80\u5316\u6a21\u5f0f\u5339\u914d\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5b83\u4f7f\u7528\u6a21\u5f0f\u5339\u914d\u6765\u786e\u5b9a\u8f93\u5165\u5bf9\u8c61\u662f\u5426\u4e3a\u7279\u5b9a\u7c7b\u578b\uff1a<\/p>\n<pre>public class PatternMatching {\n    public static void main(String[] args) {\n        Object obj = \"Hello\";\n\n        if (obj instanceof String str) {\n            System.out.println(\"obj is a String: \" + str);\n        }\n    }\n}<\/pre>\n<p><strong>\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f<\/strong><\/p>\n<p>\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\u4f7f\u7528\u5bf9\u8c61\u7684\u884c\u4e3a\u8fdb\u884c\u89e3\u8026\uff0c\u4ece\u800c\u5141\u8bb8\u66f4\u6362\u6216\u4fee\u6539\u884c\u4e3a\u800c\u4e0d\u5f71\u54cd\u5ba2\u6237\u7aef\u3002\u51fd\u6570\u5f0f\u63a5\u53e3\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u7b56\u7565\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5355\u4e00\u7684\u3001\u660e\u786e\u5b9a\u4e49\u7684\u63a5\u53e3\uff0c\u65b9\u4fbf\u7b56\u7565\u7684\u4ea4\u6362\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5b83\u4f7f\u7528\u51fd\u6570\u5f0f\u63a5\u53e3\u5b9e\u73b0\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\u6765\u8ba1\u7b97\u6298\u6263\uff1a<\/p>\n<pre>public class StrategyDesignPattern {\n    public static void main(String[] args) {\n        double price = 100;\n\n        \/\/ \u5b9a\u4e49\u4e0d\u540c\u7684\u6298\u6263\u7b56\u7565\n        DiscountStrategy normalDiscount = x -&gt; x - x * 0.1;\n        DiscountStrategy premiumDiscount = x -&gt; x - x * 0.2;\n\n        \/\/ \u6839\u636e\u4e0d\u540c\u7684\u60c5\u51b5\u9009\u62e9\u4e0d\u540c\u7684\u7b56\u7565\n        double discount = price * (userType == \"Premium\" ? premiumDiscount : normalDiscount);\n\n        \/\/ \u8ba1\u7b97\u6298\u6263\u540e\u4ef7\u683c\n        double discountedPrice = price - discount;\n        System.out.println(\"Discounted price: \" + discountedPrice);\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aJava\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6a21\u5f0f\u5339\u914d\u548c\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u4f18\u52bf\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6a21\u5f0f\u5339\u914d\u548c\u7b56&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202544","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202544"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202544\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}