我是Java的新手,只是学习OOP概念.请查看我的代码.我收到以下错误.-隐式超级构造函数未定义.
class BoxSuper
{
int height;
int length;
int width;
BoxSuper(BoxSuper obj)
{
height=obj.height;
length=obj.length;
width=obj.width;
}
BoxSuper(int a,int b,int c)
{
height=a;
length=b;
width=c;
}
BoxSuper(int val)
{
height=length=width=val;
}
int volume()
{
return height*length*width;
}
}
class BoxSub extends BoxSuper
{
int weight;
BoxSub(int a,int b,int c,int d)
{
height=a;
length=b;
width=c;
weight=d;
}
}
解决方法:
您收到此错误,因为BoxSuper没有no-arg构造函数.在BoxSub中构造函数调用期间,如果没有定义超级构造函数,则调用Java尝试自动调用no-arg super()构造函数.
在BoxSuper中定义一个超级构造函数调用,如下所示:
class BoxSub extends BoxSuper
{
int weight;
BoxSub(int a,int b,int c,int d)
{
super(a, b, c);
weight=d;
}
}
或者在BoxSuper中定义一个无参数的构造函数:
class BoxSuper
{
int height;
int length;
int width;
BoxSuper(){}
...
标签:java,constructor,inheritance
来源: https://codeday.me/bug/20190712/1442318.html