2010-09-20 65 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; 
}