{"id":64370,"date":"2025-05-03T17:08:06","date_gmt":"2025-05-03T09:08:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64370\/"},"modified":"2025-05-03T17:08:06","modified_gmt":"2025-05-03T09:08:06","slug":"java%e6%80%8e%e4%b9%88%e5%ae%9a%e4%b9%89%e4%b8%80%e4%b8%aa%e5%87%bd%e6%95%b0%e6%8c%87%e9%92%88%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64370\/","title":{"rendered":"java\u600e\u4e48\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u901a\u8fc7\u63a5\u53e3\u6765\u5b9e\u73b0\uff0c\u53ef\u4ee5\u901a\u8fc7 lambda \u8868\u8fbe\u5f0f\u6216\u65b9\u6cd5\u5f15\u7528\u6765\u5b9a\u4e49\u4e00\u4e2a\u6307\u5411\u7279\u5b9a\u7b7e\u540d\u65b9\u6cd5\u7684\u51fd\u6570\u6307\u9488\u6570\u7ec4\u3002\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u8c03\u7528\u6307\u5411\u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a\u901a\u8fc7 lambda \u8868\u8fbe\u5f0f\uff1amyfunc[] funcs = new myfunc[] { () -&gt; system.out.println(&#8220;hello!&#8221;), () -&gt; system.out.println(&#8220;world!&#8221;) };\u901a\u8fc7\u65b9\u6cd5\u5f15\u7528\uff1amyf\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110307272735646.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u51fd\u6570\u6307\u9488\u6570\u7ec4<\/strong><\/p>\n<p><strong>\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u6570\u7ec4\uff1f<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u51fd\u6570\u6307\u9488\u672c\u8d28\u4e0a\u662f\u63a5\u53e3\uff0c\u53ef\u4ee5\u6307\u5411\u5177\u6709\u7279\u5b9a\u7b7e\u540d\u7684\u65b9\u6cd5\u3002\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u6216\u65b9\u6cd5\u5f15\u7528\u3002<\/p>\n<p><strong>\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>interface MyFunc {\n    void call();\n}\n\nMyFunc[] funcs = new MyFunc[] {\n    () -&gt; System.out.println(\"Hello!\"),\n    () -&gt; System.out.println(\"World!\")\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5\u5f15\u7528<\/strong><\/p>\n<pre>interface MyFunc {\n    void call();\n}\n\nclass MyClass {\n    public static void hello() {\n        System.out.println(\"Hello!\");\n    }\n}\n\nMyFunc[] funcs = new MyFunc[] {\n    MyClass::hello,\n    System.out::println\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6570\u7ec4\u7684\u7528\u6cd5<\/strong><\/p>\n<p>\u5b9a\u4e49\u51fd\u6570\u6307\u9488\u6570\u7ec4\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8c03\u7528\u6307\u5411\u7684\u65b9\u6cd5\u3002<\/p>\n<pre>for (MyFunc func : funcs) {\n    func.call();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>Hello!\nWorld!<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 java \u4e2d\uff0c\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u901a\u8fc7\u63a5\u53e3\u6765\u5b9e\u73b0\uff0c\u53ef\u4ee5\u901a\u8fc7 lambda \u8868\u8fbe\u5f0f\u6216\u65b9\u6cd5\u5f15\u7528\u6765\u5b9a\u4e49\u4e00\u4e2a\u6307\u5411\u7279\u5b9a\u7b7e\u540d\u65b9\u6cd5\u7684\u51fd\u6570\u6307\u9488\u6570\u7ec4\u3002\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u8c03\u7528\u6307\u5411\u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a\u901a\u8fc7 lambda \u8868\u8fbe\u5f0f\uff1amyfunc[] funcs = new myfunc[] { () -&gt; system.out.println(&#8220;hello!&#8221;), () -&gt; system.out.println(&#8220;world!&#8221;) };\u901a\u8fc7\u65b9\u6cd5\u5f15\u7528\uff1amyf Java \u51fd\u6570\u6307\u9488\u6570\u7ec4 \u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u6570\u7ec4\uff1f \u5728 Java \u4e2d\uff0c\u51fd\u6570\u6307\u9488\u672c\u8d28\u4e0a\u662f\u63a5\u53e3\uff0c\u53ef\u4ee5\u6307\u5411\u5177\u6709\u7279\u5b9a\u7b7e\u540d\u7684\u65b9\u6cd5\u3002\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u6216\u65b9\u6cd5\u5f15\u7528\u3002 \u4f7f\u7528 lambda \u8868\u8fbe\u5f0f \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b interface MyFunc { void call(); } MyFunc[] funcs = new MyFunc[] { () -&gt; System.out.println(&#8220;Hello!&#8221;), () -&gt; System.out.println(&#8220;World!&#8221;) }; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528\u65b9\u6cd5\u5f15\u7528 interface MyFunc { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-64370","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=64370"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64370\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}