{"id":202611,"date":"2025-05-22T10:43:59","date_gmt":"2025-05-22T02:43:59","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202611\/"},"modified":"2025-05-22T10:43:59","modified_gmt":"2025-05-22T02:43:59","slug":"%e7%94%a8-java-%e4%b8%ad%e7%9a%84%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e5%ae%9e%e7%8e%b0%e5%8f%af%e6%b5%8b%e8%af%95%e6%80%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202611\/","title":{"rendered":"\u7528 Java \u4e2d\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u5b9e\u73b0\u53ef\u6d4b\u8bd5\u6027"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u7528 Java \u4e2d\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u5b9e\u73b0\u53ef\u6d4b\u8bd5\u6027<\/h1>\n<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u7528 Java \u4e2d\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u5b9e\u73b0\u53ef\u6d4b\u8bd5\u6027\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p>\u4f7f\u7528 Java \u4e2d\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u5b9e\u73b0\u53ef\u6d4b\u8bd5\u6027\uff1a\u4f7f\u7528\u4e0d\u53ef\u53d8\u503c\u548c\u7eaf\u51fd\u6570\uff1a\u7eaf\u51fd\u6570\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u4f1a\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u4f7f\u4ee3\u7801\u6613\u4e8e\u6d4b\u8bd5\u3002\u4f7f\u7528\u51fd\u6570\u5f0f\u4ee3\u7801\u8ba1\u7b97\u9636\u4e58\uff1a\u4f7f\u7528\u8fed\u4ee3\u548c reduce \u51fd\u6570\u5b9e\u73b0\u51fd\u6570\u5f0f\u9636\u4e58\u8ba1\u7b97\uff0c\u65e0\u9700\u9012\u5f52\u3002\u4f7f\u7528\u8868\u6d4b\u8bd5\u51fd\u6570\u5f0f\u4ee3\u7801\uff1a\u901a\u8fc7\u521b\u5efa\u5305\u542b\u9884\u671f\u7ed3\u679c\u548c\u8f93\u5165\u503c\u7684\u8868\uff0c\u8f7b\u677e\u6d4b\u8bd5\u51fd\u6570\u5f0f\u4ee3\u7801\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729923804671c8adc606b6.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u7528 Java \u4e2d\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u5b9e\u73b0\u53ef\u6d4b\u8bd5\u6027<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u4f8b\uff0c\u5b83\u5f3a\u8c03\u4f7f\u7528\u4e0d\u53ef\u53d8\u503c\u548c\u7eaf\u51fd\u6570\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u5bb9\u6613\u6d4b\u8bd5\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u786e\u4fe1\u7eaf\u51fd\u6570\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u4f1a\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b Java \u7c7b\uff0c\u5b83\u8ba1\u7b97\u4e00\u4e2a\u6570\u5b57\u7684\u9636\u4e58\uff1a<\/p>\n<pre>public class Factorial {\n\n    public static int factorial(int n) {\n        if (n &lt;= 1) {\n            return 1;\n        }\n        return n * factorial(n - 1);\n    }\n}<\/pre>\n<p>\u867d\u7136\u8fd9\u4e2a\u7c7b\u63d0\u4f9b\u4e86\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u4f46\u662f\u5b83\u5f88\u96be\u6d4b\u8bd5\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528\u4e86\u9012\u5f52\uff0c\u5e76\u4e14\u96be\u4ee5\u6a21\u62df\u65b9\u6cd5\u8c03\u7528\u6808\u3002\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u3002<\/p>\n<p><strong>\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u5b9e\u73b0\u9636\u4e58\u8ba1\u7b97<\/strong><\/p>\n<pre>import java.util.function.Function;\nimport java.util.stream.IntStream;\n\npublic class FactorialWithFunctionalProgramming {\n\n    public static int factorial(int n) {\n        Function&lt;Integer, Integer&gt; factorialFunction = num -&gt; num &lt;= 1 ? 1 : num * factorialFunction.apply(num - 1);\n        return IntStream.iterate(n, factorialFunction).limit(n).reduce((a, b) -&gt; a * b).getAsInt();\n    }\n}<\/pre>\n<p><strong>\u6d4b\u8bd5\u51fd\u6570\u5f0f\u4ee3\u7801<\/strong><\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u7684 factorial \u65b9\u6cd5\u5c31\u662f\u4e00\u4e2a\u7eaf\u51fd\u6570\uff0c\u5b83\u53ea\u4f9d\u8d56\u4e8e\u5176\u8f93\u5165\u3002\u4e3a\u4e86\u6d4b\u8bd5\u5b83\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u521b\u5efa\u4e00\u4e2a\u8868\uff0c\u5176\u4e2d\u5305\u542b\u9884\u671f\u7ed3\u679c\u548c\u8f93\u5165\u503c\uff1a<\/p>\n<pre>import org.junit.jupiter.api.Test;\n\npublic class FactorialWithFunctionalProgrammingTest {\n\n    @Test\n    void testFactorial() {\n        int n = 5;\n        int expected = 120;\n        int actual = FactorialWithFunctionalProgramming.factorial(n);\n        assertEquals(expected, actual);\n    }\n}<\/pre>\n<p><strong>\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u597d\u5904<\/strong><\/p>\n<ul>\n<li><strong>\u53ef\u9884\u6d4b\u6027\uff1a<\/strong>\u7eaf\u51fd\u6570\u59cb\u7ec8\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u5bb9\u6613\u9884\u6d4b\u3002<\/li>\n<li><strong>\u53ef\u7ec4\u5408\u6027\uff1a<\/strong>\u51fd\u6570\u5f0f\u4ee3\u7801\u53ef\u4ee5\u50cf\u79ef\u6728\u4e00\u6837\u7ec4\u5408\uff0c\u8fd9\u4f7f\u5f97\u521b\u5efa\u590d\u6742\u7684\u903b\u8f91\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002<\/li>\n<li><strong>\u6d4b\u8bd5\u65b9\u4fbf\u6027\uff1a<\/strong>\u51fd\u6570\u5f0f\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u8868\u6765\u8f7b\u677e\u6d4b\u8bd5\uff0c\u5176\u4e2d\u5305\u542b\u9884\u671f\u7ed3\u679c\u548c\u8f93\u5165\u503c\u3002\u8fd9\u6d88\u9664\u4e86\u6a21\u62df\u65b9\u6cd5\u8c03\u7528\u6808\u7684\u9700\u8981\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 Java \u4e2d\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u5b9e\u73b0&#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-202611","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202611","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=202611"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202611\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}