标签:rgs length not his sel cts 结果 temp 空间
对象数组:一组相关的对象,使用时要先开辟空间,此时数组里每一个对象都是null值,使用时数组中的每一个对象必须分别进行实例化。
1 package tryprj; 2 3 public class Temp { 4 5 public static void main(String[] args) { 6 7 /** 声明一个对象数组,包含三个对象 */ 8 System.out.println("---动态初始化---"); 9 10 Person[] person = new Person[3]; 11 12 for (int x = 0; x < person.length; x++) { 13 System.out.println(person[x] + " "); 14 } 15 16 /** 创建三个对象并实例化 */ 17 Person p1 = new Person("熊大", 1); 18 Person p2 = new Person("熊二", 2); 19 Person p3 = new Person("萌萌", 3); 20 21 person[0] = p1; 22 person[1] = p2; 23 person[2] = p3; 24 25 for (int x = 0; x < person.length; x++) { 26 System.out.println(person[x] + " "); 27 } 28 29 System.out.println("---静态初始化---"); 30 31 Person[] person2 = { new Person("熊大", 1), new Person("熊二", 2), new Person("萌萌", 3) }; 32 33 for (int x = 0; x < person2.length; x++) { 34 System.out.println(person2[x] + " "); 35 } 36 37 } 38 39 } 40 41 class Person { 42 43 /** 姓名年龄属性 */ 44 private String name; 45 private int age; 46 47 /** 构造方法赋值 */ 48 public Person(String name, int age) { 49 50 this.name = name; 51 this.age = age; 52 } 53 54 public String toString() { 55 56 return "[name=" + name + ", age=" + age + "]"; 57 } 58 }
结果如下:
---动态初始化---
null
null
null
[name=熊大, age=1]
[name=熊二, age=2]
[name=萌萌, age=3]
---静态初始化---
[name=熊大, age=1]
[name=熊二, age=2]
[name=萌萌, age=3]
[20-04-23][Self-study Notes 9]Java Array of Objects
标签:rgs length not his sel cts 结果 temp 空间
原文地址:https://www.cnblogs.com/mirai3usi9/p/12763940.html