2010-09-20 64 views
1
class A 
{ 

    int i,j; 

    A (int x, int y) 
    { 

     i = x; 
     j = y; 
    } 

    void show() 
    { 

     System.out.println("the value of i and j are " + i + " " + j); 
    } 
} 

class B extends A // here is the error 

{ 
    int k; 

    B (int z, int a, int b) 
    { 

     k = z; 
     i = a; 
     j = b; 
    } 

    void display() 
    { 

     System.out.println("the value of k is " +k); 
    } 

public static void main (String args[]) { 

     A a = new A(5,10); 
     /*a.i = 5; 
     a.j = 10; */ 
     a.show(); 

     B b = new B(2,4,6); 

     /*b.i = 2 ; 
     b.j = 4; 
     b.k = 6;*/ 

     b.show(); 
     b.display(); } 
} 
+0

您是否在B文件中導入了您的A類? – vodkhang 2010-09-20 08:15:15

+1

我發現最終有什麼問題,但是你的問題可能會更清晰。請閱讀http://tinyurl.com/so-hints – 2010-09-20 08:16:44

+0

您是否將這些類添加到同一個文件?請注意,在Java中,所有類都需要在.java文件中。同時確保您擁有正確的大寫字母。你也必須編譯A.java和B.java – Thirler 2010-09-20 08:17:12

回答

7

您的B構造函數需要調用A中的構造函數。默認情況下,它會嘗試調用一個無參數的構造函數,但是你沒有 - 因此錯誤。正確的修復方法是使用super調用參數化的一個:

B (int z, int a, int b) 
{ 
    super(a, b);  
    k = z; 
}