{"id":202910,"date":"2025-05-22T11:28:34","date_gmt":"2025-05-22T03:28:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202910\/"},"modified":"2025-05-22T11:28:34","modified_gmt":"2025-05-22T03:28:34","slug":"%e5%a6%82%e4%bd%95%e7%bb%93%e5%90%88java%e5%87%bd%e6%95%b0%e9%87%8d%e8%bd%bd%e5%92%8c%e7%bb%a7%e6%89%bf%e5%ae%9e%e7%8e%b0%e5%a4%9a%e6%80%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202910\/","title":{"rendered":"\u5982\u4f55\u7ed3\u5408Java\u51fd\u6570\u91cd\u8f7d\u548c\u7ee7\u627f\u5b9e\u73b0\u591a\u6001"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u7ed3\u5408Java\u51fd\u6570\u91cd\u8f7d\u548c\u7ee7\u627f\u5b9e\u73b0\u591a\u6001<\/h1>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u7ed3\u5408Java\u51fd\u6570\u91cd\u8f7d\u548c\u7ee7\u627f\u5b9e\u73b0\u591a\u6001\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p>\u51fd\u6570\u91cd\u8f7d\u548c\u7ee7\u627f\u5b9e\u73b0\u591a\u6001\uff1a\u51fd\u6570\u91cd\u8f7d\uff1a\u5728\u540c\u7c7b\u4e2d\u5b9a\u4e49\u540c\u540d\u51fd\u6570\uff0c\u53c2\u6570\u4e0d\u540c\uff0c\u7f16\u8bd1\u5668\u81ea\u52a8\u9009\u62e9\u8c03\u7528\u54ea\u4e2a\u51fd\u6570\u3002\u7ee7\u627f\uff1a\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u65b9\u6cd5\uff0c\u5b50\u7c7b\u53ef\u8986\u5199\u7236\u7c7b\u65b9\u6cd5\u5b9e\u73b0\u4e0d\u540c\u5b9e\u73b0\u3002\u591a\u6001\uff1a\u7236\u7c7b\u5f15\u7528\u6307\u5411\u5b50\u7c7b\u5bf9\u8c61\u65f6\uff0c\u8c03\u7528\u7236\u7c7b\u65b9\u6cd5\u4f1a\u6267\u884c\u5b50\u7c7b\u4e2d\u8986\u5199\u7684\u65b9\u6cd5\uff0c\u4e0d\u540c\u5bf9\u8c61\u901a\u8fc7\u7236\u7c7b\u5f15\u7528\u7edf\u4e00\u8c03\u7528\u65b9\u6cd5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729995163671da19b890c4.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u91cd\u8f7d\u4e0e\u7ee7\u627f\u5b9e\u73b0\u591a\u6001<\/strong><\/p>\n<p><strong>\u51fd\u6570\u91cd\u8f7d<\/strong><\/p>\n<p>\u51fd\u6570\u91cd\u8f7d\u662f\u6307\u5728\u540c\u4e2a\u7c7b\u4e2d\u5b9a\u4e49\u591a\u4e2a\u5177\u6709\u76f8\u540c\u540d\u79f0\u4f46\u53c2\u6570\u4e0d\u540c\u7684\u51fd\u6570\u3002\u5f53\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u6839\u636e\u5b9e\u9645\u4f20\u9012\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u81ea\u52a8\u9009\u62e9\u8981\u8c03\u7528\u7684\u51fd\u6570\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>class Shape {\n    void draw(int radius) {\n        System.out.println(\"Drawing a circle with radius: \" + radius);\n    }\n\n    void draw(int length, int width) {\n        System.out.println(\"Drawing a rectangle with length: \" + length + \", width: \" + width);\n    }\n}<\/pre>\n<p><strong>\u7ee7\u627f<\/strong><\/p>\n<p>\u7ee7\u627f\u662f\u6307\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u4e2d\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5b50\u7c7b\u53ef\u4ee5\u8986\u5199\u7236\u7c7b\u4e2d\u7684\u65b9\u6cd5\uff0c\u4e14\u8986\u5199\u540e\u7684\u65b9\u6cd5\u5177\u6709\u4e0d\u540c\u7684\u5b9e\u73b0\u3002\u8fd9\u662f\u5b9e\u73b0\u591a\u6001\u7684\u57fa\u7840\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>class Circle extends Shape {\n    @Override\n    void draw(int radius) {\n        System.out.println(\"Drawing a circle with radius: \" + radius);\n    }\n}\n\nclass Rectangle extends Shape {\n    @Override\n    void draw(int length, int width) {\n        System.out.println(\"Drawing a rectangle with length: \" + length + \", width: \" + width);\n    }\n}<\/pre>\n<p><strong>\u591a\u6001<\/strong><\/p>\n<p>\u591a\u6001\u662f\u6307\u7236\u7c7b\u5f15\u7528\u6307\u5411\u5b50\u7c7b\u5bf9\u8c61\u65f6\uff0c\u8c03\u7528\u7236\u7c7b\u4e2d\u7684\u65b9\u6cd5\uff0c\u5b9e\u9645\u6267\u884c\u7684\u662f\u5b50\u7c7b\u4e2d\u8986\u5199\u540e\u7684\u65b9\u6cd5\u3002\u8fd9\u6837\uff0c\u4e0d\u540c\u7684\u5b50\u7c7b\u5bf9\u8c61\u53ef\u4ee5\u901a\u8fc7\u7236\u7c7b\u5f15\u7528\u6765\u7edf\u4e00\u8c03\u7528\u65b9\u6cd5\uff0c\u5b9e\u73b0\u591a\u6001\u884c\u4e3a\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>public class Main {\n    public static void main(String[] args) {\n        Shape circle = new Circle();\n        Shape rectangle = new Rectangle();\n\n        circle.draw(5); \/\/ \u8c03\u7528\u5b50\u7c7b Circle \u4e2d\u7684 draw \u65b9\u6cd5\n        rectangle.draw(10, 15); \/\/ \u8c03\u7528\u5b50\u7c7b Rectangle \u4e2d\u7684 draw \u65b9\u6cd5\n    }\n}<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>Drawing a circle with radius: 5\nDrawing a rectangle with length: 10, width: 15<\/pre>\n<p>\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c<code>Shape<\/code> \u7c7b\u578b\u7684\u53d8\u91cf\u540c\u65f6\u6307\u5411 <code>Circle<\/code> \u548c <code>Rectangle<\/code> \u5bf9\u8c61\uff0c\u4f46\u8c03\u7528 <code>draw<\/code> \u65b9\u6cd5\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u6839\u636e\u5b9e\u9645\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u81ea\u52a8\u9009\u62e9\u6b63\u786e\u7684\u5b9e\u73b0\u3002\u8fd9\u5c31\u662f\u591a\u6001\u7684\u4f53\u73b0\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u7ed3\u5408Java\u51fd\u6570\u91cd\u8f7d\u548c\u7ee7\u627f\u5b9e\u73b0\u591a\u6001\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7ee7\u627f,\u591a\u6001\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7ed3\u5408Java\u51fd\u6570\u91cd\u8f7d\u548c\u7ee7\u627f\u5b9e&#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-202910","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202910","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=202910"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202910\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}