2013-12-21 79 views
0

我遇到以下警告,但沒有線索如何處理它。 正如你所看到的,我有一個[課程]類,具有通用屬性[idOrName]。 當我嘗試創建inseretion方法得到以下worning:通用警告 - 沒有類型的封閉實例

No enclosing instance of type PR3 is accessible. Must qualify the 
allocation with an enclosing instance of type PR3 (e.g. x.new A() 
where x is an instance of PR3). 

其中PR3是類名。

以下是代碼,警告是在行set.add(new Course(name,avg));

private static void insertCoursesToSet(Set<Course> set, int n, int it) { 
     Scanner s = new Scanner(System.in); 

     if(it==1) 
      for(int i=0 ; i<n ; i++){ 
       System.out.println("Please enter name:"); 
       s.nextLine(); //clear buffer 
       String name = s.nextLine(); 
       System.out.println("Please enter avg:"); 
       float avg = s.nextFloat(); 
       set.add(new Course<String>(name,avg)); 
      } 
     else 
      for(int i=0 ; i<n ; i++){ 
       System.out.println("Please enter id:"); 
       Integer id = s.nextInt(); 
       System.out.println("Please enter avg:"); 
       float avg = s.nextFloat(); 
       set.add(new Course<Integer>(id,avg)); 
      } 
    } 

    public class Course<E>{ 
     private E idOrName; 
     private float avg; 

     public Course(E idOrName, float avg){ 
      this.idOrName = idOrName; 
      this.avg = avg; 
     } 
    } 
+0

難道你用課程'課程'創建單獨的文件? –

+0

大括號的縮進幾乎沒有任何意義...請問您是否可以解決這個問題,我很謹慎地建議自己編輯,因爲您可能按照自己的想法以不同的方式進行編輯。 – skiwi

+1

請參閱此答案以獲取有關錯誤究竟是什麼的更多信息:http://stackoverflow.com/questions/9744639/must-qualify-the-allocation-with-an-enclosing-instance-of-type-geolocation?rq= 1 – skiwi

回答

2

這是因爲你創建內部類CoursePR3insertCoursesToSet方法是static

如果你感興趣的把它作爲內部類,使其static也:

public static class Course<E>{ 
    private E idOrName; 
    private float avg; 

    public Course(E idOrName, float avg){ 
     this.idOrName = idOrName; 
     this.avg = avg; 
    } 
} 

OR

insertCoursesToSet非靜態的,你可以寫:

private void insertCoursesToSet(Set<Course> set, int n, int it) { 
    Scanner s = new Scanner(System.in); 

    if(it==1) 
     for(int i=0 ; i<n ; i++){ 
      System.out.println("Please enter name:"); 
      s.nextLine(); //clear buffer 
      String name = s.nextLine(); 
      System.out.println("Please enter avg:"); 
      float avg = s.nextFloat(); 
      set.add(new Course<String>(name,avg)); 
     } 
    else 
     for(int i=0 ; i<n ; i++){ 
      System.out.println("Please enter id:"); 
      Integer id = s.nextInt(); 
      System.out.println("Please enter avg:"); 
      float avg = s.nextFloat(); 
      set.add(new Course<Integer>(id,avg)); 
     } 
} 

public class Course<E>{ 
    private E idOrName; 
    private float avg; 

    public Course(E idOrName, float avg){ 
     this.idOrName = idOrName; 
     this.avg = avg; 
    } 
} 
相關問題