对象数组

 1 /*
 2 题目:
 3 定义一个数组,用来存储3个Person对象。
 4 
 5 数组有一个缺点:一旦创建,程序运行期间长度不可以发生改变。
 6  */
 7 public class Demo01Array {
 8 
 9     public static void main(String[] args) {
10         // 首先创建一个长度为3的数组,里面用来存放Person类型的对象
11         Person[] array = new Person[3];
12 
13         Person one = new Person("迪丽热巴", 18);
14         Person two = new Person("古力娜扎", 28);
15         Person three = new Person("玛尔扎哈", 38);
16 
17         // 将one当中的地址值赋值到数组的0号元素位置
18         array[0] = one;
19         array[1] = two;
20         array[2] = three;
21 
22         System.out.println(array[0]); // 地址值
23         System.out.println(array[1]); // 地址值
24         System.out.println(array[2]); // 地址值
25 
26         System.out.println(array[1].getName()); // 古力娜扎
27     }
28 
29 }

 

posted @ 2020-10-13 20:42  Oooooooa  阅读(256)  评论(0)    收藏  举报