{"id":203093,"date":"2025-05-22T11:26:38","date_gmt":"2025-05-22T03:26:38","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203093\/"},"modified":"2025-05-22T11:26:38","modified_gmt":"2025-05-22T03:26:38","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e4%b8%8e%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e7%bc%96%e7%a8%8b%e6%9c%89%e4%bd%95%e6%af%94%e8%be%83","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203093\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e0e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u6709\u4f55\u6bd4\u8f83"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e0e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u6709\u4f55\u6bd4\u8f83<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e0e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u6709\u4f55\u6bd4\u8f83\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p>Java \u4e2d\u51fd\u6570\u5f0f\u7f16\u7a0b (FP) \u548c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b (OOP) \u5177\u6709\u4e0d\u540c\u7684\u8bbe\u8ba1\u539f\u5219\uff1aOOP \u56f4\u7ed5\u5bf9\u8c61\u548c\u7c7b\u7ec4\u7ec7\u4ee3\u7801\uff0c\u5f3a\u8c03\u5c01\u88c5\uff0c\u800c FP \u5f3a\u8c03\u4e0d\u53d8\u6027\u548c\u51fd\u6570\u7684\u91cd\u7528\u6027\u3002\u4ee3\u7801\u7ed3\u6784\u4e0a\uff0cOOP \u5177\u6709\u5c42\u6b21\u7ed3\u6784\uff0cFP \u503e\u5411\u4e8e\u4e0d\u53ef\u53d8\u6570\u636e\u7ed3\u6784\u548c\u7eaf\u51fd\u6570\u3002\u6027\u80fd\u65b9\u9762\uff0cOOP \u901a\u5e38\u5177\u6709\u66f4\u4f4e\u7684\u5185\u5b58\u5f00\u9500\uff0c\u800c FP \u53ef\u4ee5\u5229\u7528\u5e76\u884c\u5316\u548c\u60f0\u6027\u6c42\u503c\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730082248671ef5c8dffe3.jpg\" class=\"aligncenter\"><\/p>\n<h2>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e0e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u5bf9\u6bd4<\/h2>\n<p>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u548c\u51fd\u6570\u5f0f\u7f16\u7a0b\uff08FP\uff09\u662f Java \u4e2d\u4e24\u79cd\u4e0d\u540c\u7684\u7f16\u7a0b\u65b9\u6cd5\u3002\u5b83\u4eec\u5728\u8bbe\u8ba1\u539f\u5219\u3001\u4ee3\u7801\u7ed3\u6784\u548c\u6027\u80fd\u65b9\u9762\u5b58\u5728\u663e\u7740\u5dee\u5f02\u3002<\/p>\n<p><strong>\u8bbe\u8ba1\u539f\u5219<\/strong><\/p>\n<ul>\n<li><strong>OOP\uff1a<\/strong> \u56f4\u7ed5\u5bf9\u8c61\u548c\u7c7b\u7ec4\u7ec7\u4ee3\u7801\uff0c\u5f3a\u8c03\u5c01\u88c5\u3001\u7ee7\u627f\u548c\u591a\u6001\u6027\u3002<\/li>\n<li><strong>FP\uff1a<\/strong> \u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u53ef\u7ec4\u5408\u7684\u53ef\u91cd\u7528\u51fd\u6570\uff0c\u5f3a\u8c03\u4e0d\u53d8\u6027\u3001\u60f0\u6027\u6c42\u503c\u548c\u5f15\u7528\u900f\u660e\u6027\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u7ed3\u6784<\/strong><\/p>\n<ul>\n<li><strong>OOP\uff1a<\/strong> \u5177\u6709\u7c7b\u548c\u65b9\u6cd5\u7684\u5c42\u6b21\u7ed3\u6784\uff0c\u6570\u636e\u88ab\u5c01\u88c5\u5728\u5bf9\u8c61\u4e2d\u3002<\/li>\n<li><strong>FP\uff1a<\/strong> \u503e\u5411\u4e8e\u4f7f\u7528\u4e0d\u53ef\u53d8\u6570\u636e\u7ed3\u6784\u548c\u7eaf\u51fd\u6570\uff0c\u6570\u636e\u4ee5\u51fd\u6570\u4f5c\u7528\u57df\u4e3a\u57fa\u7840\u3002<\/li>\n<\/ul>\n<p><strong>\u6027\u80fd<\/strong><\/p>\n<ul>\n<li><strong>OOP\uff1a<\/strong> \u901a\u5e38\u5177\u6709\u66f4\u4f4e\u7684\u5185\u5b58\u5f00\u9500\uff0c\u4f46\u53ef\u80fd\u4f1a\u51fa\u73b0\u8fc7\u5ea6\u5c01\u88c5\u548c\u65b9\u6cd5\u8c03\u7528\u7684\u5f00\u9500\u3002<\/li>\n<li><strong>FP\uff1a<\/strong> \u901a\u5e38\u5177\u6709\u66f4\u9ad8\u7684\u5185\u5b58\u6d88\u8017\uff0c\u4f46\u53ef\u4ee5\u5229\u7528\u5e76\u884c\u5316\u548c\u60f0\u6027\u6c42\u503c\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u4f20\u7edf OOP \u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre>class Customer {\n\n    private String name;\n    private int age;\n\n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    public int getAge() {\n        return age;\n    }\n\n    public void setAge(int age) {\n        this.age = age;\n    }\n}<\/pre>\n<p><strong>\u51fd\u6570\u5f0f\u7f16\u7a0b\u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre>class Customer {\n\n    public static String getName(String customer) {\n        return customer.split(\",\")[0];\n    }\n\n    public static int getAge(String customer) {\n        return Integer.parseInt(customer.split(\",\")[1]);\n    }\n}<\/pre>\n<p>\u5728\u51fd\u6570\u5f0f\u65b9\u6cd5\u4e2d\uff0c<code>getName<\/code> \u548c <code>getAge<\/code> \u662f\u4f5c\u4e3a\u7eaf\u51fd\u6570\u5b9e\u73b0\u7684\uff0c\u5b83\u4eec\u63a5\u53d7 <code>String<\/code> \u8f93\u5165\u5e76\u8fd4\u56de\u4e0d\u53d8\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e0e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u6709\u4f55\u6bd4\u8f83\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>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e0e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b&#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-203093","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203093","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=203093"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203093\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}