package com;
/**
* 学生类
*
*/
public class Student {
private String name;
private int age;
public Student(){}
public Student(String name,int age){
this.name = name;
this.age = age;
}
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 "Student [name=" + name + ", age=" + age + "]";
}
}
package com;
/**
* 我有5个学生,请把这5个学生的信息存储到数组中,并遍历数组,获取到每一个学生信息
* 学生:Student
* 成员变量:name,age
* 构造方法:无参,有参
* 成员方法:setter/getter方法
* 分析:
* 1.创建学生类
* 2.创建学生数组(对象数组)
* 3.创建5个学生对象,并赋值
* 4.把3步骤的元素,放到数组中
* 5.遍历学生数组
*
*/
public class ObjectArrayDemo {
public static void main(String[] args) {
//创建学生数组
Student[] stus = new Student[5];
//创建5个学生对象,并赋值
Student stu1 = new Student("林青霞",27);
Student stu2 = new Student("风清扬",30);
Student stu3 = new Student("刘意",30);
Student stu4 = new Student("赵雅芝",60);
Student stu5 = new Student("王力宏",35);
//把学生对象存储到数组中
stus[0] = stu1;
stus[1] = stu2;
stus[2] = stu3;
stus[3] = stu4;
stus[4] = stu5;
for (int i = 0; i
System.out.println("学生信息:"+stus[i]);
}
}
}
学生信息:Student [name=林青霞, age=27]
学生信息:Student [name=风清扬, age=30]
学生信息:Student [name=刘意, age=30]
学生信息:Student [name=赵雅芝, age=60]
学生信息:Student [name=王力宏, age=35]