{"id":39405,"date":"2024-11-26T15:06:06","date_gmt":"2024-11-26T07:06:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39405\/"},"modified":"2024-11-26T15:06:06","modified_gmt":"2024-11-26T07:06:06","slug":"java%e9%87%8c%e6%80%8e%e4%b9%88%e7%94%a8%e5%88%ab%e7%9a%84%e7%b1%bb%e7%9a%84%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39405\/","title":{"rendered":"java\u91cc\u600e\u4e48\u7528\u522b\u7684\u7c7b\u7684\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4f7f\u7528\u5176\u4ed6\u7c7b\u7684\u6570\u7ec4\uff1a\u521b\u5efa\u6570\u7ec4\u5bf9\u8c61\u5e76\u6307\u5b9a\u7c7b\u578b\u548c\u5927\u5c0f\u3002\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u5206\u914d\u503c\u5e76\u521d\u59cb\u5316\u6570\u7ec4\u3002\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u5b58\u50a8\u548c\u8bbf\u95ee\u5176\u4ed6\u7c7b\u5bf9\u8c61\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111607211910612.jpg\" class=\"aligncenter\" title=\"java\u91cc\u600e\u4e48\u7528\u522b\u7684\u7c7b\u7684\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u91cc\u600e\u4e48\u7528\u522b\u7684\u7c7b\u7684\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u5176\u4ed6\u7c7b\u7684\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u6570\u7ec4\u5bf9\u8c61\u5e76\u5c06\u5176\u521d\u59cb\u5316\u4e3a\u5176\u4ed6\u7c7b\u7684\u5bf9\u8c61\u6765\u4f7f\u7528\u5176\u4ed6\u7c7b\u7684\u6570\u7ec4\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p><strong>\u6b65\u9aa4 1\uff1a\u521b\u5efa\u6570\u7ec4\u5bf9\u8c61<\/strong><\/p>\n<p>\u4f7f\u7528 new \u5173\u952e\u5b57\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\u5bf9\u8c61\uff0c\u5e76\u6307\u5b9a\u6570\u7ec4\u7684\u7c7b\u578b\u548c\u5927\u5c0f\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>OtherClass[] otherClassArray = new OtherClass[arraySize];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0cOtherClass \u662f\u5176\u4ed6\u7c7b\u7684\u540d\u79f0\uff0carraySize \u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/p>\n<p><strong>\u6b65\u9aa4 2\uff1a\u521d\u59cb\u5316\u6570\u7ec4<\/strong><\/p>\n<p>\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u4e3a\u6570\u7ec4\u5143\u7d20\u5206\u914d\u503c\uff1a<\/p>\n<pre>otherClassArray[index] = new OtherClass();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0cindex \u662f\u8981\u5206\u914d\u503c\u7684\u6570\u7ec4\u7d22\u5f15\u3002<\/p>\n<p><strong>\u6b65\u9aa4 3\uff1a\u4f7f\u7528\u6570\u7ec4<\/strong><\/p>\n<p>\u4e00\u65e6\u6570\u7ec4\u5df2\u521d\u59cb\u5316\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u50a8\u548c\u8bbf\u95ee\u5176\u4ed6\u7c7b\u5bf9\u8c61\uff1a<\/p>\n<ul>\n<li> <strong>\u5b58\u50a8\u5bf9\u8c61\uff1a<\/strong>\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u5c06\u5bf9\u8c61\u5b58\u50a8\u5230\u6570\u7ec4\u4e2d\uff1a<\/li>\n<\/ul>\n<pre>otherClassArray[index] = myOtherClassObject;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u8bbf\u95ee\u5bf9\u8c61\uff1a<\/strong>\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u4ece\u6570\u7ec4\u4e2d\u83b7\u53d6\u5bf9\u8c61\uff1a<\/li>\n<\/ul>\n<pre>OtherClass object = otherClassArray[index];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u5176\u4ed6\u7c7b\u6570\u7ec4\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>public class Main {\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u4e00\u4e2a Person \u7c7b\u7684\u6570\u7ec4\n        Person[] peopleArray = new Person[3];\n\n        \/\/ \u521d\u59cb\u5316\u6570\u7ec4\n        peopleArray[0] = new Person(\"John\", 30);\n        peopleArray[1] = new Person(\"Mary\", 25);\n        peopleArray[2] = new Person(\"Bob\", 40);\n\n        \/\/ \u4f7f\u7528\u6570\u7ec4\n        for (Person person : peopleArray) {\n            System.out.println(person);\n        }\n    }\n}\n\nclass Person {\n    private String name;\n    private int age;\n\n    public Person(String name, int age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    @Override\n    public String toString() {\n        return \"Name: \" + name + \", Age: \" + age;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>Name: John, Age: 30\nName: Mary, Age: 25\nName: Bob, Age: 40<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u91cc\u600e\u4e48\u7528\u522b\u7684\u7c7b\u7684\u6570\u7ec4\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\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4f7f\u7528\u5176\u4ed6\u7c7b\u7684\u6570\u7ec4\uff1a\u521b\u5efa\u6570\u7ec4\u5bf9\u8c61\u5e76\u6307\u5b9a\u7c7b\u578b\u548c\u5927\u5c0f\u3002\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u5206\u914d\u503c\u5e76\u521d\u59cb\u5316\u6570\u7ec4\u3002\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u5b58\u50a8\u548c\u8bbf\u95ee\u5176\u4ed6\u7c7b\u5bf9\u8c61\u3002 \u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u5176\u4ed6\u7c7b\u7684\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u6570\u7ec4\u5bf9\u8c61\u5e76\u5c06\u5176\u521d\u59cb\u5316\u4e3a\u5176\u4ed6\u7c7b\u7684\u5bf9\u8c61\u6765\u4f7f\u7528\u5176\u4ed6\u7c7b\u7684\u6570\u7ec4\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a \u6b65\u9aa4 1\uff1a\u521b\u5efa\u6570\u7ec4\u5bf9\u8c61 \u4f7f\u7528 new \u5173\u952e\u5b57\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\u5bf9\u8c61\uff0c\u5e76\u6307\u5b9a\u6570\u7ec4\u7684\u7c7b\u578b\u548c\u5927\u5c0f\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b OtherClass[] otherClassArray = new OtherClass[arraySize]; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0cOtherClass \u662f\u5176\u4ed6\u7c7b\u7684\u540d\u79f0\uff0carraySize \u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002 \u6b65\u9aa4 2\uff1a\u521d\u59cb\u5316\u6570\u7ec4 \u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u4e3a\u6570\u7ec4\u5143\u7d20\u5206\u914d\u503c\uff1a otherClassArray[index] = new OtherClass(); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0cindex \u662f\u8981\u5206\u914d\u503c\u7684\u6570\u7ec4\u7d22\u5f15\u3002 \u6b65\u9aa4 3\uff1a\u4f7f\u7528\u6570\u7ec4 \u4e00\u65e6\u6570\u7ec4\u5df2\u521d\u59cb\u5316\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u50a8\u548c\u8bbf\u95ee\u5176\u4ed6\u7c7b\u5bf9\u8c61\uff1a \u5b58\u50a8\u5bf9\u8c61\uff1a\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u5c06\u5bf9\u8c61\u5b58\u50a8\u5230\u6570\u7ec4\u4e2d\uff1a otherClassArray[index] = myOtherClassObject; \u767b\u5f55\u540e\u590d\u5236 \u8bbf\u95ee\u5bf9\u8c61\uff1a\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u4ece\u6570\u7ec4\u4e2d\u83b7\u53d6\u5bf9\u8c61\uff1a OtherClass object = otherClassArray[index]; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a \u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u5176\u4ed6\u7c7b\u6570\u7ec4\u7684\u793a\u4f8b\uff1a public class Main { public [&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-39405","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39405","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=39405"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39405\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}