{"id":36963,"date":"2024-11-26T14:11:03","date_gmt":"2024-11-26T06:11:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36963\/"},"modified":"2024-11-26T14:11:03","modified_gmt":"2024-11-26T06:11:03","slug":"java%e6%8e%a5%e5%8f%a3%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36963\/","title":{"rendered":"java\u63a5\u53e3\u6570\u7ec4\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u63a5\u53e3\u6570\u7ec4\u7528\u4e8e\u5b58\u50a8\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u7684\u4efb\u4f55\u7c7b\u7684\u5bf9\u8c61\u3002\u5176\u4f18\u70b9\u5305\u62ec\u591a\u6001\u6027\u3001\u89e3\u8026\u548c\u4ee3\u7801\u91cd\u7528\u3002\u4f7f\u7528\u8bed\u6cd5\uff1a\u63a5\u53e3\u540d[] \u63a5\u53e3\u6570\u7ec4\u540d = new \u63a5\u53e3\u540d[\u6570\u7ec4\u5927\u5c0f]\uff0c\u4f8b\u5982\uff1adrawable[] shapes = new drawable[2]\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111222243653467.jpg\" class=\"aligncenter\" title=\"java\u63a5\u53e3\u6570\u7ec4\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"java\u63a5\u53e3\u6570\u7ec4\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u63a5\u53e3\u6570\u7ec4\u7684\u4f7f\u7528<\/strong><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u63a5\u53e3\u6570\u7ec4\uff1f<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u58f0\u660e\u4e00\u4e2a\u63a5\u53e3\u7c7b\u578b\u7684\u6570\u7ec4\u6765\u4f7f\u7528\u63a5\u53e3\u6570\u7ec4\u3002\u63a5\u53e3\u6570\u7ec4\u53ef\u4ee5\u5b58\u50a8\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u7684\u4efb\u4f55\u7c7b\u7684\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\u63a5\u53e3\u540d[] \u63a5\u53e3\u6570\u7ec4\u540d = new \u63a5\u53e3\u540d[\u6570\u7ec4\u5927\u5c0f];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public interface Drawable {\n    void draw();\n}\n\npublic class Circle implements Drawable {\n    @Override\n    public void draw() {\n        System.out.println(\"Drawing a circle.\");\n    }\n}\n\npublic class Square implements Drawable {\n    @Override\n    public void draw() {\n        System.out.println(\"Drawing a square.\");\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) {\n        Drawable[] shapes = new Drawable[2];\n        shapes[0] = new Circle();\n        shapes[1] = new Square();\n\n        for (Drawable shape : shapes) {\n            shape.draw();\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u591a\u6001\u6027\uff1a<\/strong>\u63a5\u53e3\u6570\u7ec4\u5141\u8bb8\u5b58\u50a8\u4e0d\u540c\u7c7b\u7684\u5bf9\u8c61\uff0c\u5e76\u5728\u8fd0\u884c\u65f6\u5bf9\u5b83\u4eec\u8fdb\u884c\u7edf\u4e00\u5904\u7406\u3002<\/li>\n<li> <strong>\u89e3\u8026\uff1a<\/strong>\u5b83\u5c06\u63a5\u53e3\u4e0e\u5177\u4f53\u7684\u5b9e\u73b0\u7c7b\u89e3\u8026\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u3002<\/li>\n<li> <strong>\u4ee3\u7801\u91cd\u7528\uff1a<\/strong>\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u63a5\u53e3\u6570\u7ec4\u6765\u5b9e\u73b0\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u7684\u7edf\u4e00\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u63a5\u53e3\u6570\u7ec4\u53ea\u80fd\u5b58\u50a8\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u7c7b\u7684\u5bf9\u8c61\u3002<\/li>\n<li>\u65e0\u6cd5\u5728\u63a5\u53e3\u6570\u7ec4\u4e2d\u5b58\u50a8\u539f\u59cb\u6570\u636e\u7c7b\u578b\uff08\u5982 int\u3001double\uff09\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u7ec4\u600e\u4e48\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 java \u4e2d\uff0c\u63a5\u53e3\u6570\u7ec4\u7528\u4e8e\u5b58\u50a8\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u7684\u4efb\u4f55\u7c7b\u7684\u5bf9\u8c61\u3002\u5176\u4f18\u70b9\u5305\u62ec\u591a\u6001\u6027\u3001\u89e3\u8026\u548c\u4ee3\u7801\u91cd\u7528\u3002\u4f7f\u7528\u8bed\u6cd5\uff1a\u63a5\u53e3\u540d[] \u63a5\u53e3\u6570\u7ec4\u540d = new \u63a5\u53e3\u540d[\u6570\u7ec4\u5927\u5c0f]\uff0c\u4f8b\u5982\uff1adrawable[] shapes = new drawable[2]\u3002 Java \u63a5\u53e3\u6570\u7ec4\u7684\u4f7f\u7528 \u5982\u4f55\u4f7f\u7528 Java \u63a5\u53e3\u6570\u7ec4\uff1f \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u58f0\u660e\u4e00\u4e2a\u63a5\u53e3\u7c7b\u578b\u7684\u6570\u7ec4\u6765\u4f7f\u7528\u63a5\u53e3\u6570\u7ec4\u3002\u63a5\u53e3\u6570\u7ec4\u53ef\u4ee5\u5b58\u50a8\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u7684\u4efb\u4f55\u7c7b\u7684\u5bf9\u8c61\u3002 \u8bed\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u63a5\u53e3\u540d[] \u63a5\u53e3\u6570\u7ec4\u540d = new \u63a5\u53e3\u540d[\u6570\u7ec4\u5927\u5c0f]; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a public interface Drawable { void draw(); } public class Circle implements Drawable { @Override public void draw() { System.out.println(&#8220;Drawing a circle.&#8221;); } } public class Square implements [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-36963","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=36963"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36963\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}