{"id":202609,"date":"2025-05-22T11:21:06","date_gmt":"2025-05-22T03:21:06","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202609\/"},"modified":"2025-05-22T11:21:06","modified_gmt":"2025-05-22T03:21:06","slug":"java-%e6%96%b9%e6%b3%95%e5%bc%95%e7%94%a8%e7%9a%84%e5%b7%a5%e4%bd%9c%e5%8e%9f%e7%90%86%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202609\/","title":{"rendered":"Java \u65b9\u6cd5\u5f15\u7528\u7684\u5de5\u4f5c\u539f\u7406\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u65b9\u6cd5\u5f15\u7528\u7684\u5de5\u4f5c\u539f\u7406\u662f\u4ec0\u4e48\uff1f<\/h1>\n<p><span style=\"font-size: 15px\">\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava \u65b9\u6cd5\u5f15\u7528\u7684\u5de5\u4f5c\u539f\u7406\u662f\u4ec0\u4e48\uff1f\u300b<\/span>\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/span><\/p>\n<p>\u65b9\u6cd5\u5f15\u7528\u662f\u4e00\u79cd\u5c06\u65b9\u6cd5\u8f6c\u6362\u4e3a\u5bf9\u8c61\u7684\u8bed\u6cd5\u7ed3\u6784\uff0c\u5b83\u4f7f\u7528\u88ab\u79f0\u4e3a FunctionalInterface \u7684\u63a5\u53e3\u7c7b\u578b\u548c :: \u7b26\u53f7\u6765\u521b\u5efa\u6307\u5411\u65b9\u6cd5\u7684\u5f15\u7528\uff0c\u5e76\u5e7f\u6cdb\u5e94\u7528\u4e8e\u4e8b\u4ef6\u5904\u7406\u3001\u6d41\u64cd\u4f5c\u548c\u51fd\u6570\u5f0f\u7f16\u7a0b\u7b49\u573a\u666f\u3002\u65b9\u6cd5\u5f15\u7528\u7684\u4f18\u70b9\u5305\u62ec\u7b80\u6d01\u6027\u3001\u53ef\u8bfb\u6027\u3001\u7c7b\u578b\u5b89\u5168\u6027\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729923447671c8977d64c9.jpg\" class=\"aligncenter\"><\/p>\n<h2>Java \u65b9\u6cd5\u5f15\u7528\uff1a\u63ed\u79d8\u5176\u5de5\u4f5c\u539f\u7406<\/h2>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u65b9\u6cd5\u5f15\u7528\u662f Java 8 \u4e2d\u5f15\u5165\u7684\u4e00\u4e2a\u7b80\u6d01\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u521b\u5efa\u6307\u5411\u65b9\u6cd5\u7684\u5f15\u7528\u3002\u4e0e\u533f\u540d\u5185\u90e8\u7c7b\u4e0d\u540c\uff0c\u65b9\u6cd5\u5f15\u7528\u662f\u4e00\u79cd\u66f4\u7b80\u6d01\u4e14\u6613\u4e8e\u9605\u8bfb\u7684\u8bed\u6cd5\u7ed3\u6784\u3002<\/p>\n<p><strong>\u5de5\u4f5c\u539f\u7406<\/strong><\/p>\n<p>\u65b9\u6cd5\u5f15\u7528\u672c\u8d28\u4e0a\u662f\u5c06\u65b9\u6cd5\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u3002\u5b83\u4f7f\u7528 Java \u7c7b\u578b\u7cfb\u7edf\u4e2d\u7684 FunctionalInterface\uff08\u51fd\u6570\u5f0f\u63a5\u53e3\uff09\uff0c\u8be5\u63a5\u53e3\u5177\u6709\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\uff0c\u4e0e\u76ee\u6807\u65b9\u6cd5\u7684\u7b7e\u540d\u5339\u914d\u3002\u901a\u8fc7\u4f7f\u7528 <code>::<\/code> \u7b26\u53f7\uff0c\u53ef\u4ee5\u5c06\u65b9\u6cd5\u540d\u79f0\u9644\u52a0\u5230\u63a5\u53e3\u7c7b\u578b\u4e0a\uff0c\u4ece\u800c\u521b\u5efa\u65b9\u6cd5\u5f15\u7528\uff1a<\/p>\n<pre>\/\/ \u65b9\u6cd5\u5f15\u7528\nInterfaceType::methodName<\/pre>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<p>\u65b9\u6cd5\u5f15\u7528\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u573a\u666f\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u4e8b\u4ef6\u5904\u7406\uff1a<\/strong>\u5c06\u65b9\u6cd5\u5f15\u7528\u7528\u4f5c\u4e8b\u4ef6\u76d1\u542c\u5668\u3002<\/li>\n<li><strong>\u6d41\u64cd\u4f5c\uff1a<\/strong>\u5728\u6d41\u64cd\u4f5c\u4e2d\u4f20\u9012\u65b9\u6cd5\u5f15\u7528\u4f5c\u4e3a\u53c2\u6570\u3002<\/li>\n<li><strong>\u51fd\u6570\u5f0f\u7f16\u7a0b\uff1a<\/strong>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u4f7f\u7528\u65b9\u6cd5\u5f15\u7528\u6765\u521b\u5efa\u51fd\u6570\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u573a\u666f\uff1a<\/strong>\u9700\u8981\u5728\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4e2d\u8c03\u7528 <code>show()<\/code> \u65b9\u6cd5\u3002<\/p>\n<p><strong>\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\uff1a<\/strong><\/p>\n<pre>\/\/ \u533f\u540d\u5185\u90e8\u7c7b\nJButton button = new JButton();\nbutton.addActionListener(new ActionListener() {\n    public void actionPerformed(ActionEvent e) {\n        show();\n    }\n});<\/pre>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5\u5f15\u7528\uff1a<\/strong><\/p>\n<pre>\/\/ \u65b9\u6cd5\u5f15\u7528\nJButton button = new JButton();\nbutton.addActionListener(this::show);<\/pre>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<p>\u65b9\u6cd5\u5f15\u7528\u7684\u4f18\u70b9\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u7b80\u6d01\u6027\uff1a<\/strong>\u6bd4\u533f\u540d\u5185\u90e8\u7c7b\u66f4\u7b80\u6d01\u3002<\/li>\n<li><strong>\u53ef\u8bfb\u6027\uff1a<\/strong>\u66f4\u6613\u4e8e\u9605\u8bfb\u548c\u7406\u89e3\u3002<\/li>\n<li><strong>\u7c7b\u578b\u5b89\u5168\u6027\uff1a<\/strong>\u7f16\u8bd1\u5668\u786e\u4fdd\u65b9\u6cd5\u7b7e\u540d\u4e0e\u63a5\u53e3\u7c7b\u578b\u5339\u914d\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300aJava \u65b9\u6cd5\u5f15\u7528\u7684\u5de5\u4f5c\u539f\u7406\u662f\u4ec0\u4e48\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8ejava,\u65b9\u6cd5\u5f15\u7528\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u65b9\u6cd5\u5f15\u7528\u7684\u5de5\u4f5c\u539f\u7406\u662f\u4ec0&#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-202609","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202609","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=202609"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202609\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}