{"id":202628,"date":"2025-05-22T09:57:57","date_gmt":"2025-05-22T01:57:57","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202628\/"},"modified":"2025-05-22T09:57:57","modified_gmt":"2025-05-22T01:57:57","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e4%b8%8d%e5%90%8c%e9%a1%b9%e7%9b%ae%e4%b8%ad%e6%9c%89%e6%95%88%e5%9c%b0%e9%87%8d%e7%94%a8-java-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202628\/","title":{"rendered":"\u5982\u4f55\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u6709\u6548\u5730\u91cd\u7528 Java \u51fd\u6570"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u6709\u6548\u5730\u91cd\u7528 Java \u51fd\u6570<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u6709\u6548\u5730\u91cd\u7528 Java \u51fd\u6570\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p>Java \u4e2d\u51fd\u6570\u91cd\u7528\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u6548\u7387\uff0c\u901a\u8fc7\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u5171\u4eab\u51fd\u6570\u5b9e\u73b0\uff1a\u521b\u5efa\u91cd\u7528\u51fd\u6570\uff1a\u5b9a\u4e49\u4e00\u4e2a\u72ec\u7acb\u7c7b\uff0c\u5305\u542b\u51fd\u6570\u5e76\u58f0\u660e\u4e3a static \u548c public\u3002\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u4f7f\u7528\uff1a\u5bfc\u5165\u5305\u542b\u51fd\u6570\u7684\u7c7b\uff0c\u5e76\u4f7f\u7528\u7c7b\u540d.\u51fd\u6570\u540d\u8bed\u6cd5\u8c03\u7528\u51fd\u6570\u3002\u5b9e\u6218\u6848\u4f8b\uff1aAreaCalculator \u7c7b\u5305\u542b\u8ba1\u7b97\u9762\u79ef\u7684\u51fd\u6570\uff0c\u53ef\u901a\u8fc7\u5bfc\u5165\u7c7b\u5e76\u8c03\u7528 calculateArea \u51fd\u6570\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u4f7f\u7528\u3002\u597d\u5904\uff1a\u6d88\u9664\u91cd\u590d\u4ee3\u7801\u3001\u63d0\u9ad8\u6548\u7387\u3001\u589e\u5f3a\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729925667671c92233cb11.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 Java \u4e2d\u91cd\u7528\u51fd\u6570\uff0c\u6253\u9020\u9ad8\u6548\u4ee3\u7801<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u91cd\u7528\u51fd\u6570\u662f Java \u4e2d\u63d0\u9ad8\u4ee3\u7801\u6548\u7387\u7684\u5173\u952e\u5b9e\u8df5\u3002\u901a\u8fc7\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u5171\u4eab\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8282\u7701\u65f6\u95f4\u3001\u51cf\u5c11\u4ee3\u7801\u91cd\u590d\uff0c\u5e76\u63d0\u9ad8\u7ef4\u62a4\u6027\u3002<\/p>\n<p><strong>\u521b\u5efa\u91cd\u7528\u51fd\u6570<\/strong><\/p>\n<ul>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u5355\u72ec\u7684 Java \u7c7b\u6765\u5305\u542b\u51fd\u6570\u3002<\/li>\n<li>\u5c06\u51fd\u6570\u58f0\u660e\u4e3a <code>static<\/code> \u548c <code>public<\/code> \u4ee5\u4f7f\u8be5\u51fd\u6570\u80fd\u591f\u4ece\u5176\u4ed6\u7c7b\u4e2d\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n<pre>public class UtilityFunctions {\n\n  public static int add(int a, int b) {\n    return a + b;\n  }\n\n}<\/pre>\n<p><strong>\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u4f7f\u7528\u91cd\u7528\u51fd\u6570<\/strong><\/p>\n<ul>\n<li>\u5728\u9700\u8981\u7684\u4f4d\u7f6e\u5bfc\u5165\u5305\u542b\u51fd\u6570\u7684\u7c7b\u3002<\/li>\n<li>\u4f7f\u7528 <code>\u7c7b\u540d.\u51fd\u6570\u540d<\/code> \u8bed\u6cd5\u8c03\u7528\u51fd\u6570\u3002<\/li>\n<\/ul>\n<pre>\/\/ \u5728\u53e6\u4e00\u9879\u76ee\u4e2d\u4f7f\u7528\nimport static utility.UtilityFunctions.add;\n\nint result = add(5, 7);<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u4f7f\u7528\u4e00\u4e2a\u8ba1\u7b97\u9762\u79ef\u7684\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u7c7b <code>AreaCalculator<\/code> \u6765\u5305\u542b\u8be5\u51fd\u6570\u3002<\/p>\n<pre>public class AreaCalculator {\n\n  public static double calculateArea(Shape shape) {\n    if (shape instanceof Circle) {\n      return Math.PI * ((Circle) shape).getRadius() * ((Circle) shape).getRadius();\n    } else if (shape instanceof Square) {\n      return ((Square) shape).getLength() * ((Square) shape).getLength();\n    } else {\n      throw new IllegalArgumentException(\"Shape type not supported\");\n    }\n  }\n\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u5bfc\u5165 <code>AreaCalculator<\/code> \u7c7b\u5e76\u8c03\u7528 <code>calculateArea<\/code> \u51fd\u6570\u6765\u4f7f\u7528\u8be5\u51fd\u6570\u3002<\/p>\n<pre>\/\/ \u5728\u9879\u76ee A \u4e2d\u4f7f\u7528\nimport static areacalc.AreaCalculator.calculateArea;\n\ndouble area = calculateArea(new Circle(5));<\/pre>\n<pre>\/\/ \u5728\u9879\u76ee B \u4e2d\u4f7f\u7528\nimport static areacalc.AreaCalculator.calculateArea;\n\ndouble area = calculateArea(new Square(10));<\/pre>\n<p><strong>\u597d\u5904<\/strong><\/p>\n<p>\u91cd\u7528\u51fd\u6570\u5177\u6709\u4ee5\u4e0b\u597d\u5904\uff1a<\/p>\n<ul>\n<li><strong>\u6d88\u9664\u91cd\u590d\u4ee3\u7801\uff1a<\/strong>\u5b83\u901a\u8fc7\u907f\u514d\u5728\u591a\u4e2a\u9879\u76ee\u4e2d\u590d\u5236\u76f8\u540c\u7684\u4ee3\u7801\u6765\u51cf\u5c11\u4ee3\u7801\u7ef4\u62a4\u7684\u8d1f\u62c5\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u6548\u7387\uff1a<\/strong>\u901a\u8fc7\u91cd\u7528\u7ecf\u8fc7\u6d4b\u8bd5\u548c\u8c03\u8bd5\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u8282\u7701\u5f00\u53d1\u65f6\u95f4\u3002<\/li>\n<li><strong>\u589e\u5f3a\u53ef\u7ef4\u62a4\u6027\uff1a<\/strong>\u96c6\u4e2d\u7ba1\u7406\u51fd\u6570\u4f7f\u66f4\u65b0\u548c\u7ef4\u62a4\u53d8\u5f97\u66f4\u5bb9\u6613\u3002<\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u6709\u6548\u5730\u91cd\u7528 Java \u51fd\u6570\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>\u5982\u4f55\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u6709\u6548\u5730\u91cd\u7528 Ja&#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-202628","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202628","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=202628"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202628\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}