LZ的构造方法不对,再有就是对数组附值时要用你注释掉的部分,要输入学生对象时,要重写toString()方法,实现对学生对象的属性输出。
public class Student {
String name;
int age;
public Student(){}
// public void Stu(String name,int age){
// this.name=name;
// this.age=age;
// System.out.println("name="+name);
// System.out.println("age="+age);
// }
public String getName() {
return name;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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 "name="+this.name+";age="+this.age;
};
public static void main(String[] args)
{
Student[] arr=new Student[5];
// Student a=new Student("张三",20);
arr[0] = new Student("张三",20);
arr[1]= new Student("李四",21);
arr[2]=new Student("王二",22);
arr[3]=new Student("麻子",23);
arr[4]=new Student("小贱",24);
// arr[0].Stu("张三",24);
// arr[1].Stu ("李四",21);
// arr[2].Stu ("王二",22);
// arr[3].Stu ("麻子",23);
// arr[4].Stu ("小贱",24);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
|