{"id":202770,"date":"2025-05-22T08:42:10","date_gmt":"2025-05-22T00:42:10","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202770\/"},"modified":"2025-05-22T08:42:10","modified_gmt":"2025-05-22T00:42:10","slug":"java-%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e8%8c%83%e5%bc%8f%e4%b8%ad%e5%b8%b8%e8%a7%81%e7%9a%84%e9%94%99%e8%af%af%e5%92%8c%e9%99%b7%e9%98%b1","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202770\/","title":{"rendered":"Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\u548c\u9677\u9631"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\u548c\u9677\u9631<\/h1>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300aJava \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\u548c\u9677\u9631\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u4f8b\u4e2d\u7684\u5e38\u89c1\u9677\u9631\u5305\u62ec\uff1a\u526f\u4f5c\u7528\uff1a\u51fd\u6570\u4e0d\u5f97\u4fee\u6539\u5916\u90e8\u72b6\u6001\u3002\u53ef\u53d8\u6027\uff1a\u5bf9\u8c61\u5728\u521b\u5efa\u540e\u4e0d\u80fd\u4fee\u6539\uff0c\u9632\u6b62\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u7a7a\u503c\uff1a\u4f7f\u7528 Optional \u5b89\u5168\u5904\u7406\u7a7a\u503c\uff0c\u9700\u8c28\u614e\u68c0\u67e5\u7a7a\u503c\u3002\u67ef\u91cc\u5316\uff1a\u907f\u514d\u8fc7\u5ea6\u67ef\u91cc\u5316\uff0c\u4fdd\u6301\u4ee3\u7801\u6e05\u6670\u3002\u9519\u8bef\u5904\u7406\uff1a\u4f7f\u7528 Optional \u6216 Either \u8868\u793a\u9519\u8bef\uff0c\u5e76\u59a5\u5584\u5904\u7406\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729940447671ccbdf5c20e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u4f8b\u4e2d\u7684\u5e38\u89c1\u9519\u8bef\u548c\u9677\u9631<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b (FP) \u8303\u4f8b\u5f3a\u8c03\u65e0\u72b6\u6001\u51fd\u6570\u3001\u4e0d\u53ef\u53d8\u6027\u4ee5\u53ca\u4e00\u7b49\u516c\u6c11\u51fd\u6570\u3002\u867d\u7136\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4f18\u52bf\uff0c\u4f46\u5b83\u4e5f\u5f15\u5165\u4e86\u65b0\u7684\u9677\u9631\u9700\u8981\u8c28\u614e\u907f\u514d\u3002<\/p>\n<p><strong>1. \u526f\u4f5c\u7528<\/strong><\/p>\n<p>FP \u51fd\u6570\u4e0d\u5f97\u5177\u6709\u4efb\u4f55\u526f\u4f5c\u7528\uff0c\u6bd4\u5982\u4fee\u6539\u5916\u90e8\u72b6\u6001\u6216\u4e0e I\/O \u4ea4\u4e92\u3002\u8fdd\u53cd\u6b64\u539f\u5219\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u884c\u4e3a\u548c\u96be\u4ee5\u8c03\u8bd5\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>2. \u53ef\u53d8\u6027<\/strong><\/p>\n<p>\u4e0d\u53ef\u53d8\u6027\u662f FP \u7684\u6838\u5fc3\u539f\u5219\uff0c\u5b83\u9632\u6b62\u5bf9\u8c61\u5728\u521b\u5efa\u540e\u8fdb\u884c\u4fee\u6539\u3002\u8fdd\u53cd\u6b64\u539f\u5219\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u548c\u610f\u5916\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>class MutableCounter {\n    private int count;\n\n    \/\/ \u5728\u51fd\u6570\u8c03\u7528\u4e2d\u4fee\u6539\u5916\u90e8\u72b6\u6001\uff01\u8fd9\u4f1a\u5bfc\u81f4\u526f\u4f5c\u7528\u548c\u4e0d\u53ef\u9884\u77e5\u884c\u4e3a\u3002\n    public void increment() {\n        count++;\n    }\n}<\/pre>\n<p><strong>3. \u7a7a\u503c\u5f15\u7528<\/strong><\/p>\n<p>FP \u9f13\u52b1\u4f7f\u7528 Optional \u6765\u5b89\u5168\u5730\u5904\u7406\u7a7a\u503c\u3002\u7136\u800c\uff0c\u5fd8\u8bb0\u68c0\u67e5\u7a7a\u503c\u5f97 Optional \u53ef\u80fd\u5bfc\u81f4 NullPointerException\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>Optional&lt;String&gt; name = ...;\nSystem.out.println(name.get()); \/\/ \u53ef\u80fd\u629b\u51fa NullPointerException<\/pre>\n<p><strong>4. \u67ef\u91cc\u5316\u6ee5\u7528<\/strong><\/p>\n<p>\u67ef\u91cc\u5316\u5141\u8bb8\u521b\u5efa\u90e8\u5206\u5e94\u7528\u7684\u51fd\u6570\u3002\u7136\u800c\uff0c\u8fc7\u5ea6\u4f7f\u7528\u67ef\u91cc\u5316\u4f1a\u5bfc\u81f4\u4ee3\u7801\u6df7\u4e71\u548c\u96be\u4ee5\u7ef4\u62a4\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u590d\u6742\u4e14\u96be\u4ee5\u9605\u8bfb\u7684\u67ef\u91cc\u5316\u793a\u4f8b\nFunction&lt;Integer, Function&lt;Integer, Integer&gt;&gt; add =\n    x -&gt; y -&gt; x + y;<\/pre>\n<p><strong>5. \u9519\u8bef\u5904\u7406<\/strong><\/p>\n<p>FP \u51fd\u6570\u901a\u5e38\u8fd4\u56de Optional \u6216 Either \u6765\u8868\u793a\u9519\u8bef\u3002\u5fd8\u8bb0\u5904\u7406\u9519\u8bef\u6216\u5904\u7406\u4e0d\u5f53\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>Optional&lt;Integer&gt; result = ...;\nif (result.isPresent()) {\n    \/\/ \u5904\u7406\u7ed3\u679c\uff0c\u4f46\u5ffd\u7565\u9519\u8bef\n}<\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u7eaf\u51fd\u6570\uff0c\u907f\u514d\u526f\u4f5c\u7528\u3002<\/li>\n<li>\u7ef4\u62a4\u4e0d\u53ef\u53d8\u6027\uff0c\u9632\u6b62\u5bf9\u8c61\u4fee\u6539\u3002<\/li>\n<li>\u8c28\u614e\u5904\u7406\u7a7a\u503c\uff0c\u4f7f\u7528 Optional\u3002<\/li>\n<li>\u9002\u5ea6\u5730\u4f7f\u7528\u67ef\u91cc\u5316\uff0c\u4f7f\u4ee3\u7801\u4fdd\u6301\u6e05\u6670\u3002<\/li>\n<li>\u6b63\u786e\u5904\u7406\u9519\u8bef\uff0c\u4f7f\u7528 Either \u6216\u5176\u4ed6 FP \u673a\u5236\u3002<\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aJava \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\u548c\u9677\u9631\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8ejava,\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f\u4e2d\u5e38\u89c1\u7684&#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-202770","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202770","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=202770"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202770\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}