0

这是我的代码:该代码应返回包含来自 arraylist 的相关数据的学生列表。但是错误告诉我它不能对非静态方法进行静态引用??

我试图使方法静态,它给了我另一个错误。

//main function code
    String forename = null;
    String surname = null;
    String grade = null;
    String yesOrNo;
    double mark;
    int selection;

ArrayList<StudentClass> studentDetails = new ArrayList<StudentClass>();

switch(selection){
case 1: {
    if (studentDetails.isEmpty()){
        System.out.println("No Students Have Been Entered Yet");
        main(null);
        break;
                }
    else{
         for(int i = 0; i < studentDetails.size(); i++){
             StudentClass = studentDetails.get(i);
             System.out.println( StudentClass.getForename() + " " +
             StudentClass.getSurname() + " received a " + StudentClass.getGrade() +
             " for their Student Mark of " + StudentClass.getMark() + "." );
            }
        }
break;
\\ Error:Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
StudentClass cannot be resolved to a variable
Cannot make a static reference to the non-static method getForename() from the   type StudentClass
Cannot make a static reference to the non-static method getSurname() from the type StudentClass
Cannot make a static reference to the non-static method getGrade() from the type StudentClass
Cannot make a static reference to the non-static method getMark() from the type StudentClass

at students.main(students.java:60)
\\code for Class
public class  StudentClass {
      public String Forename;
      public String Surname;
      public  String Grade;
       public  double Mark;

public StudentClass(String forename, String surname, double mark){

       Forename = forename;
   Surname = surname;
   Mark = mark;
   }

  public void setForename(String forename)
    {
        Forename= forename;
    }

  public void setSurname(String surname)
    {
        Surname= surname;
    }

  public void setMark(double mark)
    {
        Mark= mark;
    }

  public  String getForename()
    {
        return Forename;
    }

  public  String getSurname()
    {
        return Surname;
    }

  public  double getMark()
    {
        return Mark;
    }
  public  String getGrade()
    {   
            if ( Mark < 40 )
                Grade = "FAIL";
            else if ( (Mark >= 40) && (Mark <= 64) )
                Grade ="PASS";
            else if ( (Mark >= 65) && (Mark <= 84) )
                Grade ="MERIT";
            else if ( (Mark >= 85) && (Mark <= 100) )
                Grade ="DISTINCTION";
            return Grade;
}
  }
4

4 回答 4

1

StudentClass = studentDetails.get(i);没有意义。StudentClass是一个类名。

您需要一个实例:StudentClass student = studentDetails.get(i);然后使用student.getSurname()等。

于 2013-10-23T05:22:37.950 回答
0

StudentClass.getSurname()未定义为静态方法。您需要声明该类的实例才能调用它。

StudentClass myClass = new StudentClass(/* whatever params you need*/);
String surname = myClass.getSurname();

请记住,您需要为所有这些方法执行此操作,因为它们处于实例级别。

于 2013-10-23T05:21:56.837 回答
0
for(int i = 0; i < studentDetails.size(); i++){
             StudentClass = studentDetails.get(i);
             System.out.println( StudentClass.getForename() + " " +
             StudentClass.getSurname() + " received a " + StudentClass.getGrade() +
             " for their Student Mark of " + StudentClass.getMark() + "." );
            }

将上面的代码更改为:

    for(int i = 0; i < studentDetails.size(); i++){
        StudentClass student = studentDetails.get(i);
        System.out.println( student.getForename() + " " +
        student.getSurname() + " received a " + student.getGrade() +
                 " for their Student Mark of " + student.getMark() + "." );
   }

看看错误会不会消失。编译器抱怨是因为您试图在类本身(而不是类的实例)上调用实例级(非静态)方法。

于 2013-10-23T05:23:16.783 回答
0

没有对象就无法访问java中的非静态方法

就这样试试

else{
         for(int i = 0; i < studentDetails.size(); i++){
             studentDetails.get(i) = new StudentClass();
             System.out.println( studentDetails.get(i).getForename() + " " +
             studentDetails.get(i).getSurname() + " received a " + studentDetails.get(i).getGrade() +
             " for their Student Mark of " + studentDetails.get(i).getMark() + "." );
            }
        }
于 2013-10-23T05:25:14.713 回答