{"id":37358,"date":"2024-11-26T16:00:10","date_gmt":"2024-11-26T08:00:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37358\/"},"modified":"2024-11-26T16:00:10","modified_gmt":"2024-11-26T08:00:10","slug":"java%e6%8e%a5%e5%8f%a3%e6%95%b0%e7%bb%84%e5%8f%82%e6%95%b0%e6%80%8e%e4%b9%88%e4%bc%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37358\/","title":{"rendered":"java\u63a5\u53e3\u6570\u7ec4\u53c2\u6570\u600e\u4e48\u4f20"},"content":{"rendered":"<blockquote><p>\n  java \u63a5\u53e3\u53ef\u4ee5\u4f5c\u4e3a\u6570\u7ec4\u53c2\u6570\u4f20\u9012\uff0c\u4f20\u9012\u65b9\u5f0f\u662f\u4f7f\u7528 void mymethod(someinterface[] interfacearray) \u8bed\u6cd5\uff0c\u5176\u4e2d someinterface \u662f\u63a5\u53e3\u540d\u79f0\uff0cinterfacearray \u662f\u6570\u7ec4\u540d\u79f0\u3002\u793a\u4f8b\u4e2d\uff0c\u63a5\u53e3\u6570\u7ec4\u88ab\u521b\u5efa\u5e76\u5206\u914d\u5bf9\u8c61\uff0c\u7136\u540e\u4f20\u9012\u7ed9 mymethod()\uff0c\u8be5\u65b9\u6cd5\u4f7f\u7528\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5e76\u8c03\u7528\u5176 somemethod() \u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111304332035036.jpg\" class=\"aligncenter\" title=\"java\u63a5\u53e3\u6570\u7ec4\u53c2\u6570\u600e\u4e48\u4f20\u63d2\u56fe\" alt=\"java\u63a5\u53e3\u6570\u7ec4\u53c2\u6570\u600e\u4e48\u4f20\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u63a5\u53e3\u4f5c\u4e3a\u6570\u7ec4\u53c2\u6570\u7684\u4f20\u9012<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u63a5\u53e3\u53ef\u4ee5\u4f5c\u4e3a\u6570\u7ec4\u7684\u53c2\u6570\uff0c\u5c31\u50cf\u4efb\u4f55\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u4e00\u6837\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u5728\u51fd\u6570\u6216\u65b9\u6cd5\u4e2d\u4f20\u9012\u4e00\u7cfb\u5217\u5b9e\u73b0\u7279\u5b9a\u63a5\u53e3\u7684\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u4f20\u9012\u65b9\u5f0f<\/strong><\/p>\n<p>\u8981\u5c06\u63a5\u53e3\u4f5c\u4e3a\u6570\u7ec4\u53c2\u6570\u4f20\u9012\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>void myMethod(SomeInterface[] interfaceArray);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0cSomeInterface \u662f\u8981\u4f20\u9012\u7684\u63a5\u53e3\uff0cinterfaceArray \u662f\u8be5\u63a5\u53e3\u7684\u6570\u7ec4\u540d\u79f0\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u5c06\u4e00\u4e2a\u63a5\u53e3\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff1a<\/p>\n<pre>public class Example {\n\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u63a5\u53e3\u6570\u7ec4\n        SomeInterface[] interfaceArray = new SomeInterface[3];\n\n        \/\/ \u4e3a\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5bf9\u8c61\n        interfaceArray[0] = new ClassA();\n        interfaceArray[1] = new ClassB();\n        interfaceArray[2] = new ClassC();\n\n        \/\/ \u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u65b9\u6cd5\n        myMethod(interfaceArray);\n    }\n\n    public static void myMethod(SomeInterface[] interfaceArray) {\n        \/\/ \u4f7f\u7528\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\n        for (SomeInterface i : interfaceArray) {\n            i.someMethod();\n        }\n    }\n\n    \/\/ \u5b9a\u4e49 SomeInterface \u63a5\u53e3\n    interface SomeInterface {\n        void someMethod();\n    }\n\n    \/\/ \u5b9a\u4e49\u5b9e\u73b0 SomeInterface \u63a5\u53e3\u7684\u7c7b\n    static class ClassA implements SomeInterface {\n        @Override\n        public void someMethod() {\n            System.out.println(\"ClassA.someMethod()\");\n        }\n    }\n\n    static class ClassB implements SomeInterface {\n        @Override\n        public void someMethod() {\n            System.out.println(\"ClassB.someMethod()\");\n        }\n    }\n\n    static class ClassC implements SomeInterface {\n        @Override\n        public void someMethod() {\n            System.out.println(\"ClassC.someMethod()\");\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5f53\u8fd0\u884c\u6b64\u793a\u4f8b\u65f6\uff0c\u5b83\u5c06\u8c03\u7528 myMethod()\uff0c\u5e76\u5728\u5176\u4e2d\u6253\u5370\u7531 ClassA\u3001ClassB \u548c ClassC \u5b9e\u73b0\u7684 someMethod() \u65b9\u6cd5\u7684\u8f93\u51fa\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u7ec4\u53c2\u6570\u600e\u4e48\u4f20\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>java \u63a5\u53e3\u53ef\u4ee5\u4f5c\u4e3a\u6570\u7ec4\u53c2\u6570\u4f20\u9012\uff0c\u4f20\u9012\u65b9\u5f0f\u662f\u4f7f\u7528 void mymethod(someinterface[] interfacearray) \u8bed\u6cd5\uff0c\u5176\u4e2d someinterface \u662f\u63a5\u53e3\u540d\u79f0\uff0cinterfacearray \u662f\u6570\u7ec4\u540d\u79f0\u3002\u793a\u4f8b\u4e2d\uff0c\u63a5\u53e3\u6570\u7ec4\u88ab\u521b\u5efa\u5e76\u5206\u914d\u5bf9\u8c61\uff0c\u7136\u540e\u4f20\u9012\u7ed9 mymethod()\uff0c\u8be5\u65b9\u6cd5\u4f7f\u7528\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5e76\u8c03\u7528\u5176 somemethod() \u65b9\u6cd5\u3002 Java \u63a5\u53e3\u4f5c\u4e3a\u6570\u7ec4\u53c2\u6570\u7684\u4f20\u9012 \u5728 Java \u4e2d\uff0c\u63a5\u53e3\u53ef\u4ee5\u4f5c\u4e3a\u6570\u7ec4\u7684\u53c2\u6570\uff0c\u5c31\u50cf\u4efb\u4f55\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u4e00\u6837\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u5728\u51fd\u6570\u6216\u65b9\u6cd5\u4e2d\u4f20\u9012\u4e00\u7cfb\u5217\u5b9e\u73b0\u7279\u5b9a\u63a5\u53e3\u7684\u5bf9\u8c61\u3002 \u4f20\u9012\u65b9\u5f0f \u8981\u5c06\u63a5\u53e3\u4f5c\u4e3a\u6570\u7ec4\u53c2\u6570\u4f20\u9012\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b void myMethod(SomeInterface[] interfaceArray); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0cSomeInterface \u662f\u8981\u4f20\u9012\u7684\u63a5\u53e3\uff0cinterfaceArray \u662f\u8be5\u63a5\u53e3\u7684\u6570\u7ec4\u540d\u79f0\u3002 \u793a\u4f8b \u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u5c06\u4e00\u4e2a\u63a5\u53e3\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff1a public class Example { public static void main(String[] args) { \/\/ \u521b\u5efa\u4e00\u4e2a\u63a5\u53e3\u6570\u7ec4 SomeInterface[] interfaceArray = new SomeInterface[3]; \/\/ \u4e3a\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5bf9\u8c61 interfaceArray[0] = new ClassA(); interfaceArray[1] = new [&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-37358","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37358","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=37358"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37358\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}