找不到符号......为什么?

时间:2010-09-20 08:14:06

标签: java

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(); }
}

1 个答案:

答案 0 :(得分:7)

您的B构造函数需要在A中调用构造函数。默认情况下,它将尝试调用无参数构造函数,但您没有 - 因此会出错。正确的解决方法是使用super调用参数化的:

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