{"id":202977,"date":"2025-05-22T11:48:56","date_gmt":"2025-05-22T03:48:56","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202977\/"},"modified":"2025-05-22T11:48:56","modified_gmt":"2025-05-22T03:48:56","slug":"%e5%87%bd%e6%95%b0%e9%87%8d%e8%bd%bd%e4%b8%8e%e5%a4%9a%e6%80%81%e5%9c%a8java%e4%b8%ad%e7%9a%84%e5%8c%ba%e5%88%ab%e5%92%8c%e8%81%94%e7%b3%bb","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202977\/","title":{"rendered":"\u51fd\u6570\u91cd\u8f7d\u4e0e\u591a\u6001\u5728Java\u4e2d\u7684\u533a\u522b\u548c\u8054\u7cfb"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u51fd\u6570\u91cd\u8f7d\u4e0e\u591a\u6001\u5728Java\u4e2d\u7684\u533a\u522b\u548c\u8054\u7cfb<\/h1>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u51fd\u6570\u91cd\u8f7d\u4e0e\u591a\u6001\u5728Java\u4e2d\u7684\u533a\u522b\u548c\u8054\u7cfb\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730010749671dde7d67ac3.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u51fd\u6570\u91cd\u8f7d\u4e0e\u591a\u6001\u5728 Java \u4e2d\u7684\u533a\u522b\u548c\u8054\u7cfb<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>\u51fd\u6570\u91cd\u8f7d\u548c\u591a\u6001\u662f Java \u4e2d\u91cd\u8981\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u6982\u5ff5\u3002\u5b83\u4eec\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u7f16\u5199\u66f4\u7075\u6d3b\u3001\u66f4\u53ef\u6269\u5c55\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>\u51fd\u6570\u91cd\u8f7d<\/strong><\/p>\n<p>\u51fd\u6570\u91cd\u8f7d\u5141\u8bb8\u5728\u7c7b\u4e2d\u521b\u5efa\u5177\u6709\u76f8\u540c\u540d\u79f0\u4f46\u53c2\u6570\u4e0d\u540c\u7684\u591a\u4e2a\u65b9\u6cd5\u3002\u8fd9 erm\u00f6glicht \u591a\u4e2a\u65b9\u6cd5\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c\u4f46\u6839\u636e\u4f20\u5165\u53c2\u6570\u7684\u4e0d\u540c\u800c\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>return_type method_name(parameter_list1) {\n    \/\/ implementation\n}\n\nreturn_type method_name(parameter_list2) {\n    \/\/ implementation\n}<\/pre>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>class Calculator {\n    public int add(int a, int b) {\n        return a + b;\n    }\n\n    public double add(double a, double b) {\n        return a + b;\n    }\n}<\/pre>\n<p><strong>\u591a\u6001<\/strong><\/p>\n<p>\u591a\u6001\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u4e00\u4e2a\u5173\u952e\u6982\u5ff5\u3002\u5b83\u5141\u8bb8\u5b50\u7c7b\u7684\u5bf9\u8c61\u4ee5\u4e0e\u7236\u7c7b\u5bf9\u8c61\u76f8\u540c\u7684\u65b9\u5f0f\u88ab\u4f7f\u7528\u3002\u8fd9\u610f\u5473\u7740\u5b50\u7c7b\u53ef\u4ee5\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e0d\u540c\u7684\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>class ParentClass {\n    public void method() {\n        \/\/ implementation\n    }\n}\n\nclass ChildClass extends ParentClass {\n    @Override\n    public void method() {\n        \/\/ different implementation\n    }\n}<\/pre>\n<p><strong>\u533a\u522b<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u7279\u5f81<\/th>\n<th>\u51fd\u6570\u91cd\u8f7d<\/th>\n<th>\u591a\u6001<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u65b9\u6cd5\u540d\u79f0<\/td>\n<td>\u76f8\u540c<\/td>\n<td>\u76f8\u540c<\/td>\n<\/tr>\n<tr>\n<td>\u53c2\u6570<\/td>\n<td>\u4e0d\u540c<\/td>\n<td>\u76f8\u540c<\/td>\n<\/tr>\n<tr>\n<td>\u7ed1\u5b9a\u7684\u65f6\u95f4<\/td>\n<td>\u7f16\u8bd1\u65f6<\/td>\n<td>\u8fd0\u884c\u65f6<\/td>\n<\/tr>\n<tr>\n<td>\u6269\u5c55\u6027<\/td>\n<td>\u4ec5\u5728\u540c\u4e00\u4e2a\u7c7b\u5185<\/td>\n<td>\u652f\u6301\u8de8\u7c7b\u5c42\u6b21<\/td>\n<\/tr>\n<tr>\n<td>\u76ee\u7684<\/td>\n<td>\u4f7f\u7528\u4e0d\u540c\u7684\u53c2\u6570\u63d0\u4f9b\u76f8\u540c\u529f\u80fd<\/td>\n<td>\u63d0\u4f9b\u540c\u4e00\u79cd\u529f\u80fd\u7684\u4e0d\u540c\u5b9e\u73b0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u8054\u7cfb<\/strong><\/p>\n<p>\u51fd\u6570\u91cd\u8f7d\u548c\u591a\u6001\u90fd\u6d89\u53ca\u65b9\u6cd5\u3002\u7136\u800c\uff0c\u5b83\u4eec\u5728\u5b9e\u73b0\u65b9\u5f0f\u548c\u76ee\u7684\u4e0a\u662f\u4e0d\u540c\u7684\u3002\u51fd\u6570\u91cd\u8f7d\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff0c\u800c\u591a\u6001\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a <code>Shape<\/code> \u7c7b\uff0c\u5b83\u8868\u793a\u4e00\u4e2a\u5f62\u72b6\u3002\u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u5b50\u7c7b\uff1a<code>Rectangle<\/code> \u548c <code>Circle<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u591a\u6001\u6765\u7ed8\u5236\u8fd9\u4e9b\u5f62\u72b6\uff0c\u800c\u65e0\u9700\u62c5\u5fc3\u5b83\u4eec\u7684\u5177\u4f53\u7c7b\u578b\u3002<\/p>\n<pre>interface Shape {\n    void draw();\n}\n\nclass Rectangle implements Shape {\n    @Override\n    public void draw() {\n        System.out.println(\"Drawing a rectangle.\");\n    }\n}\n\nclass Circle implements Shape {\n    @Override\n    public void draw() {\n        System.out.println(\"Drawing a circle.\");\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) {\n        Shape[] shapes = {new Rectangle(), new Circle()};\n        for (Shape shape : shapes) {\n            shape.draw();\n        }\n    }\n}<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>Drawing a rectangle.\nDrawing a circle.<\/pre>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u591a\u6001\u6765\u8c03\u7528\u4e0d\u540c\u7c7b\u578b\u5bf9\u8c61\u7684\u76f8\u540c\u65b9\u6cd5\uff08<code>draw()<\/code>\uff09\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u51fd\u6570\u91cd\u8f7d\u4e0e\u591a\u6001\u5728Java\u4e2d\u7684\u533a\u522b\u548c\u8054\u7cfb\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\u91cd\u8f7d\u4e0e\u591a\u6001\u5728Java\u4e2d\u7684\u533a\u522b&#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-202977","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202977","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=202977"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202977\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}