javac 的错误信息指出SrInherit1构造函数错误,不解?
为什么子类继承父类构造函数(super())可以,但子类创造自己的构造函数则会出错???
父类有自己的构造函数,
那继承该父类的子类想要有自己的构造函数
(而不是用super()继承父类构造函数)
那么以上代码该这么修改才正确呢,请“元老”指点~~
=====编译错误实例====[子类创造自己的构造函数]====
class SrInherit1
{
int x,y;
SrInherit1(int a,int b)
{
x=a;
y=b;
}
}
class SrInherit2 extends SrInherit1
{
SrInherit2(int a,int b)
{
x=a+3;
y=b+7;
}
}
public class SrInherit
{
public static void main(String args[])
{
int sum;
SrInherit2 Srv=new SrInherit2(40,80);
sum=Srv.x+Srv.y;
System.out.println("sum="+sum);
}
}
=====编译可通过实例=====[子类继承父类构造函数]===
class SrInherit1
{
int x,y;
SrInherit1(int a,int b)
{
x=a;
y=b;
}
}
class SrInherit2 extends SrInherit1
{
SrInherit2(int a,int b)
{
super(a,b);
}
}
public class SrInherit
{
public static void main(String args[])
{
int sum;
SrInherit2 Srv=new SrInherit2(40,80);
sum=Srv.x+Srv.y;
System.out.println("sum="+sum);
}
}
Please use [ code ] tag to format code listing in the future.