{"id":38165,"date":"2024-11-26T11:09:12","date_gmt":"2024-11-26T03:09:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38165\/"},"modified":"2024-11-26T11:09:12","modified_gmt":"2024-11-26T03:09:12","slug":"java%e4%b8%bb%e7%b1%bb%e6%80%8e%e4%b9%88%e8%b0%83%e7%94%a8%e5%88%86%e7%b1%bb%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38165\/","title":{"rendered":"JAVA\u4e3b\u7c7b\u600e\u4e48\u8c03\u7528\u5206\u7c7b\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u8981\u4ece\u4e3b\u7c7b\u8c03\u7528\u5206\u7c7b\u6570\u7ec4\uff0c\u9700\u8981\uff1a\u58f0\u660e\u5206\u7c7b\u6570\u7ec4\u5f15\u7528\u53d8\u91cf\u521b\u5efa\u5206\u7c7b\u6570\u7ec4\u586b\u5145\u6570\u7ec4\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111410002649212.jpg\" class=\"aligncenter\" title=\"JAVA\u4e3b\u7c7b\u600e\u4e48\u8c03\u7528\u5206\u7c7b\u6570\u7ec4\u63d2\u56fe\" alt=\"JAVA\u4e3b\u7c7b\u600e\u4e48\u8c03\u7528\u5206\u7c7b\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e3b\u7c7b\u4e2d\u8c03\u7528\u5206\u7c7b\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u5206\u7c7b\u6570\u7ec4\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u540c\u7c7b\u5bf9\u8c61\u5f15\u7528\u7684\u6570\u7ec4\u3002\u8981\u4ece\u4e3b\u7c7b\u8c03\u7528\u5206\u7c7b\u6570\u7ec4\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u58f0\u660e\u4e00\u4e2a\u5206\u7c7b\u6570\u7ec4\u5f15\u7528\u53d8\u91cf\uff1a<\/strong><\/li>\n<\/ol>\n<pre>ClassName[] arrayName;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d ClassName \u662f\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u7c7b\u578b\uff0carrayName \u662f\u6570\u7ec4\u7684\u540d\u79f0\u3002<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u4e00\u4e2a\u5206\u7c7b\u6570\u7ec4\uff1a<\/strong><\/li>\n<\/ol>\n<pre>arrayName = new ClassName[size];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d size \u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li><strong>\u586b\u5145\u6570\u7ec4\uff1a<\/strong><\/li>\n<\/ol>\n<pre>arrayName[index] = new ClassName();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d index \u662f\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0cnew ClassName() \u662f\u8981\u5b58\u50a8\u5728\u8be5\u7d22\u5f15\u5904\u7684\u5bf9\u8c61\u3002<\/p>\n<ol>\n<li><strong>\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1a<\/strong><\/li>\n<\/ol>\n<pre>ClassName element = arrayName[index];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64\u5143\u7d20\u5f15\u7528\u6765\u8c03\u7528\u5143\u7d20\u7684\u65b9\u6cd5\u6216\u8bbf\u95ee\u5176\u5b57\u6bb5\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public class Main {\n\n    public static void main(String[] args) {\n\n        \/\/ \u58f0\u660e\u4e00\u4e2a\u5b66\u751f\u5bf9\u8c61\u7684\u5206\u7c7b\u6570\u7ec4\u5f15\u7528\u53d8\u91cf\n        Student[] students;\n\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u5305\u542b 3 \u4e2a\u5b66\u751f\u5bf9\u8c61\u7684\u5206\u7c7b\u6570\u7ec4\n        students = new Student[3];\n\n        \/\/ \u586b\u5145\u6570\u7ec4\n        students[0] = new Student(\"John\", \"Doe\", 22);\n        students[1] = new Student(\"Mary\", \"Smith\", 21);\n        students[2] = new Student(\"Bob\", \"Jones\", 20);\n\n        \/\/ \u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\n        Student student = students[0];\n        System.out.println(student.getName()); \/\/ \u8f93\u51fa\uff1aJohn Doe\n    }\n}\n\nclass Student {\n\n    private String firstName;\n    private String lastName;\n    private int age;\n\n    public Student(String firstName, String lastName, int age) {\n        this.firstName = firstName;\n        this.lastName = lastName;\n        this.age = age;\n    }\n\n    public String getName() {\n        return firstName + \" \" + lastName;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJAVA\u4e3b\u7c7b\u600e\u4e48\u8c03\u7528\u5206\u7c7b\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>\u8981\u4ece\u4e3b\u7c7b\u8c03\u7528\u5206\u7c7b\u6570\u7ec4\uff0c\u9700\u8981\uff1a\u58f0\u660e\u5206\u7c7b\u6570\u7ec4\u5f15\u7528\u53d8\u91cf\u521b\u5efa\u5206\u7c7b\u6570\u7ec4\u586b\u5145\u6570\u7ec4\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20 \u5982\u4f55\u5728 Java \u4e3b\u7c7b\u4e2d\u8c03\u7528\u5206\u7c7b\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u5206\u7c7b\u6570\u7ec4\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u540c\u7c7b\u5bf9\u8c61\u5f15\u7528\u7684\u6570\u7ec4\u3002\u8981\u4ece\u4e3b\u7c7b\u8c03\u7528\u5206\u7c7b\u6570\u7ec4\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u58f0\u660e\u4e00\u4e2a\u5206\u7c7b\u6570\u7ec4\u5f15\u7528\u53d8\u91cf\uff1a ClassName[] arrayName; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d ClassName \u662f\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u7c7b\u578b\uff0carrayName \u662f\u6570\u7ec4\u7684\u540d\u79f0\u3002 \u521b\u5efa\u4e00\u4e2a\u5206\u7c7b\u6570\u7ec4\uff1a arrayName = new ClassName[size]; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d size \u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u586b\u5145\u6570\u7ec4\uff1a arrayName[index] = new ClassName(); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d index \u662f\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0cnew ClassName() \u662f\u8981\u5b58\u50a8\u5728\u8be5\u7d22\u5f15\u5904\u7684\u5bf9\u8c61\u3002 \u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1a ClassName element = arrayName[index]; \u767b\u5f55\u540e\u590d\u5236 \u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64\u5143\u7d20\u5f15\u7528\u6765\u8c03\u7528\u5143\u7d20\u7684\u65b9\u6cd5\u6216\u8bbf\u95ee\u5176\u5b57\u6bb5\u3002 \u793a\u4f8b\uff1a public class Main { public static void main(String[] args) { \/\/ [&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-38165","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38165","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=38165"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38165\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}