2017-05-01 41 views
-6

我正在學習JAVA,目前我遇到了超級用戶的問題。 出於某種原因,它會顯示錯誤:超級錯誤。在JAVA

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The field Animal.n is not visible The field Animal.m is not visible*

如何解決呢?我意識到String n和int m被設置爲private,因爲我試圖利用「超級」。先謝謝你。下面我在與問題的部分的代碼是「超級(N,M)

public class Zoo 

{ 

public static void main(String[] args) 
    { 
    Animal[] cat= new Animal[5]; 
    BigAnimal big = new BigAnimal("Roro",20,true,true); 

    cat[0] = new Animal ("Mauie",5,true); 
    cat[1] = new Animal ("Frankie",7,false); 
    cat[2] = new Animal ("Zo-Zo",8,false); 
    cat[3]= new Animal ("Alice",12,true); 
    cat[4]= big; 

    System.out.println(cat[0].getName()); 
    System.out.println(cat[0].something(10)); 

    } 



} 

class Animal 

{ 

    private String n; 
    private int m; 


    public Animal(String name,int meals, boolean doesitbite) 

    { 
     n=name; 
     m=meals; 
    } 

} 

class BigAnimal extends Animal 

{ 
    boolean doesitlove; 

    public BigAnimal (String name, int meals, boolean doesitbite, boolean doesitlove) 
     { 

      super(n,m); 
      doesitlove=true; 
     } 

} 
+7

'動物'沒有隻有2個參數的構造函數。 –

+2

*「錯誤的行(?)以粗體顯示在下面的代碼中。」*否,事實並非如此。 *「下面的代碼中的部分是粗體的,正如我所提到的那樣」*不,它不是,正如我所提到的那樣。 :-) –

+2

'超(n,m);'用'超級(名稱,飯食,無敵);' –

回答

0

構造爲BigAnimal,更改

super(n,m); 

super(name, meals) 

也就是說,將構造函數BigAnimal的值傳遞給超類構造函數。

Al所以,修正你的bug(?),或者給超類建立一個只有兩個參數的構造函數,或者將第三個參數傳遞給你所擁有的構造函數。例如,

super(name, meals, doesitbite) 
+1

好的答案,但我不會投票,因爲這會阻止OP刪除問題。這個問題對別人沒用,所以應該刪除。 – Andreas