{"id":202921,"date":"2025-05-22T10:06:33","date_gmt":"2025-05-22T02:06:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202921\/"},"modified":"2025-05-22T10:06:33","modified_gmt":"2025-05-22T02:06:33","slug":"%e5%87%bd%e6%95%b0%e5%bc%8f%e6%8e%a5%e5%8f%a3%e5%9c%a8java%e4%b8%ad%e6%9c%89%e5%93%aa%e4%ba%9b%e5%85%b7%e4%bd%93%e5%ba%94%e7%94%a8%e5%9c%ba%e6%99%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202921\/","title":{"rendered":"\u51fd\u6570\u5f0f\u63a5\u53e3\u5728Java\u4e2d\u6709\u54ea\u4e9b\u5177\u4f53\u5e94\u7528\u573a\u666f\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u51fd\u6570\u5f0f\u63a5\u53e3\u5728Java\u4e2d\u6709\u54ea\u4e9b\u5177\u4f53\u5e94\u7528\u573a\u666f\uff1f<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u51fd\u6570\u5f0f\u63a5\u53e3\u5728Java\u4e2d\u6709\u54ea\u4e9b\u5177\u4f53\u5e94\u7528\u573a\u666f\uff1f\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u5728 Java \u4e2d\u5e7f\u6cdb\u5e94\u7528\uff0c\u4e3b\u8981\u573a\u666f\u5305\u62ec\uff1a1. \u4e8b\u4ef6\u5904\u7406\uff0c\u5982\u4f7f\u7528 ActionListener \u54cd\u5e94\u4e8b\u4ef6\uff1b2. \u6570\u636e\u5904\u7406\uff0c\u5229\u7528 Stream API \u548c map() \u51fd\u6570\u8f6c\u6362\u6570\u636e\u6d41\uff1b3. \u6392\u5e8f\uff0c\u4f7f\u7528 Comparator \u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\uff1b4. \u5e76\u53d1\u7f16\u7a0b\uff0c\u4f7f\u7528 Runnable \u51fd\u6570\u521b\u5efa\u53ef\u8fd0\u884c\u4efb\u52a1\uff1b\u5177\u4f53\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u4f7f\u7528 BiFunction \u51fd\u6570\u8868\u793a\u8ba1\u7b97\u77e9\u5f62\u9762\u79ef\u7684\u51fd\u6570\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729998405671dae45486e7.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u51fd\u6570\u5f0f\u63a5\u53e3\u5728 Java \u4e2d\u7684\u5177\u4f53\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u5728 Java \u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u573a\u666f\uff1a<\/p>\n<p><strong>\u4e8b\u4ef6\u5904\u7406<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u7ecf\u5e38\u7528\u4e8e\u4e8b\u4ef6\u5904\u7406\uff0c\u5b83\u5141\u8bb8\u5c06\u884c\u4e3a\u4e0e\u4e8b\u4ef6\u5206\u79bb\u5f00\u6765\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>ActionListener<\/code> \u51fd\u6570\u5f0f\u63a5\u53e3\u6765\u54cd\u5e94\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\uff1a<\/p>\n<pre>JButton button = new JButton(\"Click Me\");\nbutton.addActionListener(e -&gt; System.out.println(\"Button clicked!\"));<\/pre>\n<p><strong>\u6570\u636e\u5904\u7406<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u8fd8\u53ef\u7528\u4e8e\u6570\u636e\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Stream<\/code> API \u4e0e <code>map()<\/code> \u51fd\u6570\u5f0f\u63a5\u53e3\u6765\u8f6c\u6362\u6570\u636e\u6d41\uff1a<\/p>\n<pre>List&lt;String&gt; names = Arrays.asList(\"John\", \"Mary\", \"Bob\");\nList&lt;String&gt; upperNames = names.stream().map(String::toUpperCase).toList();<\/pre>\n<p><strong>\u6392\u5e8f<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u4e5f\u53ef\u7528\u4e8e\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Comparator<\/code> \u51fd\u6570\u5f0f\u63a5\u53e3\u6765\u6307\u5b9a\u6392\u5e8f\u89c4\u5219\uff1a<\/p>\n<pre>List&lt;Integer&gt; numbers = Arrays.asList(1, 3, 2, 5, 4);\nnumbers.sort(Comparator.comparing(Integer::valueOf));<\/pre>\n<p><strong>\u5e76\u53d1<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u5e76\u53d1\u7f16\u7a0b\u4e2d\u4e5f\u5f88\u6709\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Runnable<\/code> \u51fd\u6570\u5f0f\u63a5\u53e3\u6765\u521b\u5efa\u53ef\u8fd0\u884c\u7684\u4efb\u52a1\uff1a<\/p>\n<pre>Thread thread = new Thread(() -&gt; System.out.println(\"Hello from a new thread!\"));\nthread.start();<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u8ba1\u7b97\u77e9\u5f62\u9762\u79ef\u7684\u7a0b\u5e8f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>BiFunction<\/code> \u51fd\u6570\u5f0f\u63a5\u53e3\u6765\u8868\u793a\u8ba1\u7b97\u9762\u79ef\u7684\u51fd\u6570\uff1a<\/p>\n<pre>import java.util.function.BiFunction;\n\nclass RectangleCalculator {\n  public static void calculateArea(double width, double height) {\n    BiFunction&lt;Double, Double, Double&gt; areaCalculator = (w, h) -&gt; w * h;\n    double area = areaCalculator.apply(width, height);\n    System.out.println(\"Area: \" + area);\n  }\n\n  public static void main(String[] args) {\n    calculateArea(4.5, 6.7);\n  }\n}<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u51fd\u6570\u5f0f\u63a5\u53e3\u5728Java\u4e2d\u6709\u54ea\u4e9b\u5177\u4f53\u5e94\u7528\u573a\u666f\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u5728Java\u4e2d\u6709\u54ea\u4e9b\u5177\u4f53&#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-202921","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202921","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=202921"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202921\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}