1、注意:
题目:定义一个数组,用来存储三个person
数组缺点:一旦创建,程序运行期间长度不能改变
2、代码实现
public static void main(String[] args) {
// TODO Auto-generated method stub
//首先创建一个长度为3的数组,用来存储person类型的对象
Person[] array = new Person[3];
String name;
int age;
Person one = new Person(name="lioyo",age=18);
Person two = new Person(name="lioyo1",age=28);
Person three = new Person(name="lioyo2",age=38);
//将one当中的地址值赋值到0号元素的位置
array[0]=one;
array[1]=two;
array[2]=three;
System.out.println(array[0]);
System.out.println(array[1].getName());
System.out.println(array[1].getAge());
}
3、运行结果
Array.Person@311d617d
lioyo1
28