public class Student
{ private String id;
private String name;
private int score;
public Student()
{
}
public Student(String id,String name,int score)
{
this.id = id;
this.name = name;
this.score= score;
}
public void setRecord(String id,String name,int score)
{
this.id = id;
this.name = name;
this.score = score;
}
public int getRecord(String id)
{
if(id.equals(this.id))
return this.score;
return 0;
}
public static void main(String[] args)
{
Student[] students = new Student[5];
students[0].setRecord("10", "wu", 51);
students[1].setRecord("11", "we", 52);
students[2].setRecord("12", "ww", 53);
students[3].setRecord("13", "wq", 54);
students[4].setRecord("14", "wt", 55);
int s = 0;
for (int i = 0; i < 5; i++)
{
s= students[i].getRecord("10");
}
System.out.println("" + s);
}}
这个代码为什么会出错呢?要怎样解决?请指导,给出正确代码。。
students[0].setRecord("10", "wu", 51);
你只是创建的放5个Student类型的数组,分配了空间!但是里面放的值都是null;students[0] = new Student();