can not find symble ... why?
class A
{
int i,j;
A (int x, int y)
{
i = x;
j = y;
}
void show()
{
System.开发者_Python百科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(); }
}
Your B
constructor needs to call a constructor in A
. By default it will try to call a parameterless constructor, but you don't have one - hence the error. The correct fix is to call the parameterized one using super
:
B (int z, int a, int b )
{
super(a, b);
k = z;
}
精彩评论