Java 继承错误:隐式超级构造函数未定义 [英] Java Inheritance error: Implicit Super Constructor is undefined
问题描述
我是 Java 的新手,只是在学习 OOP 概念.请检查我的代码.我收到以下错误.- 隐式超级构造函数未定义.
I am a novice to Java and just learning OOP concepts. Please review my code. I am getting the following error.- Implicit Super Constructor is undefined.
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 没有无参数构造函数.在 BoxSub 中调用构造函数期间,如果您没有定义超级构造函数调用,Java 会尝试自动调用无参数 super() 构造函数.
You are receiving this error because BoxSuper does not have a no-arg constructor. During your constructor call in BoxSub, if you do not define the super constructor call Java tries to automatically call the no-arg super() constructor.
或者像这样在 BoxSuper 中定义一个超级构造函数调用:
Either define a super constructor call in BoxSuper like so:
class BoxSub extends BoxSuper
{
int weight;
BoxSub(int a,int b,int c,int d)
{
super(a, b, c);
weight=d;
}
}
或者在 BoxSuper 中定义一个无参数构造函数:
or define a no-arg constructor in BoxSuper:
class BoxSuper
{
int height;
int length;
int width;
BoxSuper(){}
...
这篇关于Java 继承错误:隐式超级构造函数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!