{"id":202669,"date":"2025-05-22T11:09:04","date_gmt":"2025-05-22T03:09:04","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202669\/"},"modified":"2025-05-22T11:09:04","modified_gmt":"2025-05-22T03:09:04","slug":"%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e4%b8%8e-oop-%e7%bc%96%e7%a8%8b%e5%9c%a8-java-%e6%b5%81%e5%a4%84%e7%90%86%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202669\/","title":{"rendered":"\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e0e OOP \u7f16\u7a0b\u5728 Java \u6d41\u5904\u7406\u4e2d\u7684\u5e94\u7528"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e0e OOP \u7f16\u7a0b\u5728 Java \u6d41\u5904\u7406\u4e2d\u7684\u5e94\u7528<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e0e OOP \u7f16\u7a0b\u5728 Java \u6d41\u5904\u7406\u4e2d\u7684\u5e94\u7528\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p>\u5728 Java \u6d41\u5904\u7406\u4e2d\uff0c\u51fd\u6570\u5f0f\u548c OOP \u7f16\u7a0b\u5404\u6709\u5229\u5f0a\uff1a\u51fd\u6570\u5f0f\u7f16\u7a0b\uff1a\u5f3a\u8c03\u51fd\u6570\u5e94\u7528\u548c\u4e0d\u53ef\u53d8\u6570\u636e\uff0c\u7b80\u5316\u64cd\u4f5c\uff1bOOP \u7f16\u7a0b\uff1a\u57fa\u4e8e\u5bf9\u8c61\u548c\u7c7b\uff0c\u589e\u5f3a\u529f\u80fd\u548c\u7075\u6d3b\u6027\u3002\u5b9e\u8df5\u4e2d\uff0c\u8fd9\u4e24\u79cd\u8303\u5f0f\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\u65e5\u5fd7\u5206\u6790\u548c\u6b3a\u8bc8\u68c0\u6d4b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729929656671ca1b86a18b.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e0e OOP \u7f16\u7a0b\u5728 Java \u6d41\u5904\u7406\u4e2d\u7684\u5e94\u7528<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u548c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u662f\u7f16\u7a0b\u7684\u4e24\u79cd\u4e0d\u540c\u8303\u5f0f\u3002\u5728 Java \u6d41\u5904\u7406\u4e2d\uff0c\u8fd9\u4e24\u79cd\u8303\u5f0f\u90fd\u53ef\u4ee5\u7528\u6765\u6709\u6548\u5730\u5904\u7406\u548c\u8f6c\u6362\u6570\u636e\u3002<\/p>\n<p><strong>\u51fd\u6570\u5f0f\u7f16\u7a0b<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4fa7\u91cd\u4e8e\u51fd\u6570\u7684\u5e94\u7528\u548c\u4e0d\u53ef\u53d8\u6570\u636e\u7684\u8f6c\u6362\u3002\u5728 Java \u6d41\u5904\u7406\u4e2d\uff0c\u51fd\u6570\u5f0f\u7f16\u7a0b\u53ef\u4ee5\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u548c\u65b9\u6cd5\u5f15\u7528\u6765\u7b80\u5316\u64cd\u4f5c\u5e76\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u7b5b\u9009\u5947\u6570\nList&lt;Integer&gt; numbers = List.of(1, 2, 3, 4, 5, 6);\nList&lt;Integer&gt; oddNumbers = numbers.stream()\n    .filter(number -&gt; number % 2 != 0)\n    .toList();<\/pre>\n<p><strong>OOP \u7f16\u7a0b<\/strong><\/p>\n<p>OOP \u7f16\u7a0b\u4fa7\u91cd\u4e8e\u5bf9\u8c61\u548c\u7c7b\uff0c\u901a\u8fc7\u5c01\u88c5\u3001\u7ee7\u627f\u548c\u591a\u6001\u6027\u6765\u7ec4\u7ec7\u4ee3\u7801\u3002\u5728 Java \u6d41\u5904\u7406\u4e2d\uff0cOOP \u7f16\u7a0b\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u81ea\u5b9a\u4e49\u6d41\u548c\u6536\u96c6\u5668\u6765\u589e\u5f3a\u529f\u80fd\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4f7f\u7528\u81ea\u5b9a\u4e49\u6d41\u751f\u6210\u6590\u6ce2\u90a3\u5951\u6570\u5217\nclass FibonacciStream implements Stream&lt;Integer&gt; {\n    private int a = 0;\n    private int b = 1;\n\n    @Override\n    public Integer next() {\n        int result = a;\n        int temp = a;\n        a = b;\n        b = temp + b;\n        return result;\n    }\n\n    @Override\n    public boolean hasNext() {\n        return true; \/\/ \u65e0\u9650\u6d41\n    }\n}\n\n\/\/ \u4f7f\u7528\u81ea\u5b9a\u4e49\u6536\u96c6\u5668\u8ba1\u7b97\u5355\u8bcd\u9891\u7387\nMap&lt;String, Long&gt; wordFrequency = words.stream()\n    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u57fa\u4e8e\u6d41\u5904\u7406\u7684\u65e5\u5fd7\u5206\u6790<\/strong><\/p>\n<ul>\n<li>\u6d41\u5f0f\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u5e76\u5c06\u5176\u89e3\u6790\u4e3a\u65e5\u5fd7\u5bf9\u8c61\u3002<\/li>\n<li>\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u8fc7\u6ee4\u548c\u6620\u5c04\u65e5\u5fd7\u4e8b\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528 OOP \u7f16\u7a0b\u521b\u5efa\u81ea\u5b9a\u4e49\u6d41\u548c\u6536\u96c6\u5668\u6765\u7edf\u8ba1\u548c\u805a\u5408\u4e8b\u4ef6\u3002<\/li>\n<\/ul>\n<p><strong>\u57fa\u4e8e\u6d41\u5904\u7406\u7684\u6b3a\u8bc8\u68c0\u6d4b<\/strong><\/p>\n<ul>\n<li>\u4ece\u4e8b\u52a1\u6d41\u4e2d\u8bc6\u522b\u5f02\u5e38\u6a21\u5f0f\u3002<\/li>\n<li>\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u5bf9\u4e8b\u52a1\u8fdb\u884c\u8f6c\u6362\u548c\u7b5b\u9009\u3002<\/li>\n<li>\u4f7f\u7528 OOP \u7f16\u7a0b\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6d41\u6765\u5b9e\u65f6\u68c0\u6d4b\u6b3a\u8bc8\u6027\u4e8b\u52a1\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>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e0e OOP \u7f16\u7a0b\u5728 J&#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-202669","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202669","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=202669"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202669\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}