**对象数组的创建与初始化**
对象数组的使用步骤:
<
package z1;
public class Student {
private String name;
private int age;
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;
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
//1:创建学生数组
Student [] students=new Student[2];
//2:创建学生对象
Student s1=new Student("张三",35);
Student s2=new Student("李四",42);
//3:将学生对象作为元素赋值给学生数组
students[0]=s1;
students[1]=s2;
//4:遍历学生数组
for(int i=0;i<students.length;i++)
{
Student s=students[i];
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
tips:使用eclipse自动生成构造方法和get()、set()方法
//1:代码区右键---->Source—>Generate Constructors from super…---->无参构造方法
//2:代码区右键---->Source—>Generate Constructors using Fields…---->带参构造方法
//3:代码区右键---->Source—>Generate Getter and Setter…