2013-02-08 31 views
0

我執行顯示了這個錯誤:學習在課堂內創建對象和訪問方法我怎麼能?

java.lang.NoSuchMethodError: main

我需要讓學生詳細的輸出。該程序應該訪問所有上述方法。

import java.io.*; 
    import java.io.IOException; 

    public class Student { 
     String name; 
     int age; 
     String clas; 
     int rank; 
     int ID; 

     public Student(String name){ 
     this.name = name; 
     } 

     public void studAge(int studAge) 
     { 
      age=studAge; 
     } 
     public void studClas(String studClas) 
     { 
      clas=studClas; 
     } 
     public void studRank(int studRank) 
     { 
      rank=studRank; 
     } 
     public void studID(int studID) 
     { 
      ID=studID; 
     } 
     public void displayStud() 
     { 
      System.out.println("Student Age is:"+age); 
      System.out.println("Student Class is:"+clas); 
      System.out.println("Student Rank is:"+rank); 
      System.out.println("Student ID is:"+ID); 
     } 
    } 
    class StudRecord{ 
     public static void main(String [] args)throws IOException 
     {    
     Student studOne=new Student("Faraz"); 
     Student studTwo=new Student("Musheer"); 
     Student studThree=new Student("Imdad"); 
     Student studFour=new Student("Shahid"); 

     // Invoking methods for each object created   
     studOne.studAge(23); 
     studOne.studClas("11th"); 
     studOne.studID(130018); 
     studOne.studRank(16); 

     studFour.studAge(21); 
     studFour.studClas("11th"); 
     studFour.studID(130035); 
     studFour.studRank(33); 

     studTwo.studAge(26); 
     studTwo.studClas("10th"); 
     studTwo.studID(130021); 
     studTwo.studRank(2); 

     studThree.studAge(24); 
     studThree.studClas("11th"); 
     studThree.studID(130032); 
     studThree.studRank(32); 

     System.out.println();   
     } 
    } 

回答

0

在Java中,文件中只有一個類可能被標記爲public。在這種情況下,類Student被標記爲public,因此引發NoSuchMethodException,因爲主方法不在Student類上,而是在StudRecord類上。

因此,將StudRecord類更改爲public,並將Student類更改爲默認訪問(無訪問修飾符)。