返回值不起作用

时间:2016-04-27 09:45:11

标签: java

我是一名学习Java课程的学生。我试图使用一个对象数组和一个自定义构造函数来创建一个简单的数据库。我试图拉取我认为我设置的数据,但我一直得到null或0的响应。任何帮助将不胜感激。

打印出来的内容:

学生资料

名称:空

性别:空

家乡:空

年级:0

以英寸为单位的高度:0

package FinalProject;

public class UserClass {

public static void main(String[] args) {
StudentData[] student = new StudentData[5];

student[0] = new StudentData();
student[0].setStudentName("Bob");
student[0].setStudentGender("Male");
student[0].setStudenthometown("Elpaso");
student[0].setStudentlevel(9);
student[0].setStudentHeight(69);

student[1] = new StudentData();
student[1].setStudentName("Suzie");
student[1].setStudentGender("Female");
student[1].setStudenthometown("Dallas");
student[1].setStudentlevel(11);
student[1].setStudentHeight(44);

student[2] = new StudentData();
student[2].setStudentName("Mark");
student[2].setStudentGender("Male");
student[2].setStudenthometown("Ft Worth");
student[2].setStudentlevel(9);
student[2].setStudentHeight(54);

student[3] = new StudentData();
student[3].setStudentName("Nick");
student[3].setStudentGender("Male");
student[3].setStudenthometown("Hartland");
student[3].setStudentlevel(12);
student[3].setStudentHeight(70);

student[4] = new StudentData();
student[4].setStudentName("Mckenna");
student[4].setStudentGender("Female");
student[4].setStudenthometown("Milwaukee");
student[4].setStudentlevel(8);
student[4].setStudentHeight(56);

System.out.println("Student Data");
System.out.println("Name:"+student[1].getStudentName());
System.out.println("Gender:"+student[1].getStudentGender());
System.out.println("Hometown:"+student[1].getStudenthometown());
System.out.println("Grade Level:"+student[1].getStudentlevel());
System.out.println("Height in inches:"+student[1].getStudentheight());  
}

}


package FinalProject;

public class StudentData {
private String StudentName;
private String StudentGender;
private String Studenthometown;
private int Studentlevel;
private int Studentheight;


public String getStudentName(){
    return StudentName;
}

public String getStudentGender(){
    return StudentGender;
}

public String getStudenthometown(){
    return Studenthometown;
}
public int getStudentlevel(){
    return Studentlevel;
}

public int getStudentheight(){
    return Studentheight;
}

public void setStudentName(String string) {
    // TODO Auto-generated method stub

}

public void setStudentGender(String string) {
    // TODO Auto-generated method stub

}

public void setStudenthometown(String string) {
    // TODO Auto-generated method stub

}

public void setStudentlevel(int i) {
    // TODO Auto-generated method stub

}

public void setStudentHeight(int i) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:0)

您的setter方法不会更改StudentData类的属性。

使用类似的东西:

public void setStudentName(String string) {
    this.StudentName= string;
}
顺便说一下,您应该使用Java命名约定,这使您的代码更容易阅读,即属性名称应以小写字母开头,如

private String studentName;

当然,你必须相应地改变你的getter和setter方法。