public class Text1 { // 成员变量 private String name; private int age; //构造方法 public Text1(){ super(); } public Text1(String name,int age){ super(); this.name=name; this.age=age; } //getXxx setXxx方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Text1 [name=" + name + ", age=" + age + "]"; } }
public class Text2 { public static void main(String[] args){ //创建学生数组对象 Text1[] t=new Text1[5]; //创建5个学生对象并赋值 Text1 s1 = new Text1("小明",18); Text1 s2 = new Text1("小红",16); Text1 s3 = new Text1("小都",20); Text1 s4 = new Text1("小发",23); Text1 s5 = new Text1("小球",19); //把C步骤的元素放到数组中 t[0]=s1; t[1]=s2; t[2]=s3; t[3]=s4; t[4]=s5; //遍历 for(int i=0;i<t.length;i++){ //System.out.println(t[i]); 输出一 Text1 s=t[i]; System.out.println(s.getName()+"---"+s.getAge()); //输出二 } } }