对象数组的申明与创建
作者:互联网
package com.zhang.review;
public class Demo01 {
public static void main(String[] args) {
Person[] ps=new Person[3];
//-------------------声明对象数组--------------------
//创建三个人并实例化
Person p1=new Person("jerry",12);
Person p2=new Person("jack",19);
Person p3=new Person("mark",20);
ps[0]=p1;//实例化数组元素
ps[1]=p2;
ps[2]=p3;
System.out.println("-----------实例化数组成员-----------");
for (int i = 0; i <ps.length ; i++) {
System.out.println(ps[i]+" ");
}
}
}
class Person {
private String name;
private int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";//重写返回对象的格式
}
}
标签:ps,name,创建,age,Person,申明,数组,new,String 来源: https://www.cnblogs.com/zwkzxy/p/14907996.html