2013-10-23 109 views
0

以下是我的代碼:代碼應該返回列表中的學生與來自arraylist的相關數據。但是錯誤告訴我它不能對非靜態方法進行靜態引用?如何返回Java類中的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; 
} 
    } 

回答

0

StudentClass.getSurname()未被定義爲靜態方法。您需要聲明該類的一個實例以便能夠調用它。

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

請記住,您需要爲所有這些方法執行此操作,因爲它們處於實例級別。

1

StudentClass = studentDetails.get(i);是沒有意義的。 StudentClass是一個classname。

你需要一個實例:StudentClass student = studentDetails.get(i);然後使用student.getSurname()

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() + "."); 
    } 

看看錯誤將會消失。編譯器抱怨是因爲你試圖在類本身(而不是類的一個實例)上調用實例級別(非靜態)方法。

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() + "."); 
      } 
     }