所以我试图了解继承类。
首先,我创建了一个名为Box的类来计算盒子的区域。
然后我创建了一个TestBox类,在其中我创建了一个名为fedEx的盒子对象。
Box Class:
public class Box {
private String boxName;
public void calculateArea(int length, int width) {
System.out.println("Area of " + getBoxInfo() + (length * width));
}
public Box(String boxName) {
this.boxName = boxName;
}
public String getBoxInfo() {
return boxName;
}
}
TestBox类:
public class TestBox {
public static void main(String[] args) {
Box fedEx = new Box("fedEx");
fedEx.calculateArea(23, 2);
}
}
到目前为止,如果我运行此代码,一切正常,我的打印屏幕显示 联邦快递46区域
所以现在我去创建一个名为NewBox的新类并使用" extends"要继承Box类中的方法,该类用于计算Volume
NewBox类:
public class NewBox extends Box {
public void calculateVolume(int length, int width, int height) {
System.out.println("Volume = " + (length * width * height));
}
}
现在为了测试这个我在我的TestBox类中创建了一个名为UPS的新对象,现在我的TestBox类看起来像这样:
public class TestBox {
public static void main(String[] args) {
Box fedEx = new Box("fedEx");
fedEx.calculateArea(23, 2);
NewBox UPS = new NewBox("UPS");
UPS.calculateArea(3, 2);
UPS.calculateVolume(3, 2, 2);
}
}
当我尝试运行此程序时,收到以下错误消息:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor NewBox(String) is undefined
at day3.inheritence.TestBox.main(TestBox.java:10)
我使用eclipse作为我的IDE。
我可以做些什么来修复我的代码,错误消息是什么意思?
答案 0 :(得分:4)
NewBox必须有一个构造函数转发给父类的构造函数。试试这个:
public class NewBox extends Box{
public NewBox(String name) {
super(name);
}
public void calculateVolume(int length, int width, int height){
System.out.println("Volume = " + (length*width*height));
}
}
答案 1 :(得分:1)
在NewBox
课程中添加以下内容。
public NewBox(String name){
super(name);
}
现在NewBox
变为
public class NewBox extends Box{
public NewBox(String name){
super(name);
}
public void calculateVolume(int length, int width, int height){
System.out.println("Volume = " + (length*width*height));
}
}
答案 2 :(得分:1)
由于你的父类(Box)有一个参数化的构造函数,你的子类(NewBox)必须有一个参数化的构造函数,然后它应该调用它的超级构造函数。所以在NewBox类中添加下面的构造函数
public NewBox(String name){
super(name)
}
答案 3 :(得分:0)
让我们先谈谈default constructors。如果您不自己创建构造函数,则默认构造函数是隐式。
public class SomeClass {
// field and method declarations to follow
}
如果声明构造函数,则没有默认构造函数;只有声明的构造函数。
public class Box {
private final String boxName;
public Box(String boxName) {
this.boxName = boxName;
}
}
对于继承,如果子节点隐式声明默认构造函数但父节点不具有类似签名的构造函数,则会发生编译时错误。
更具体地说:
如果默认构造函数是隐式声明但超类没有可访问的构造函数(第6.6节),它不带参数且没有throws子句,那么这是一个编译时错误。
在您的情况下,添加super
关键字,并将参数直接传递给父级。
public class NewBox extends Box {
public NewBox(String boxName) {
super(boxName);
}
}
作为另一个例子,类Beta
将编译得很好,因为Alpha
声明了一个与默认构造函数的签名匹配的构造函数,但Delta
将无法编译,因为Gamma
没有匹配的构造函数。
class Alpha {
public Alpha() {
}
}
class Beta extends Alpha {
}
class Gamma {
private final int count;
public Gamma(int count) {
this.count = count;
}
}
class Delta extends Gamma {
}
这可以通过向`Gam
提供无参数构造函数来解决class Gamma {
private final int count;
public Gamma(int count) {
this.count = count;
}
public Gamma() {
count = 0;
}
}
答案 4 :(得分:0)
你的类NewBox应该是这样的
public class NewBox extends Box{
public NewBox(String boxName){
super(boxName);
}
public void calculateVolume(int length, int width, int height){
System.out.println("Volume = " + (length*width*height));
}
}
通过在TestBox中编写NewBox UPS = new NewBox("UPS");
, JVM期望构造函数在NewBox中使用字符串作为参数,但在那里。所以我们创建一个构造函数,它在NewBox中使用string作为参数,在那个构造函数中我们编写super(boxName)
来表示调用父类的构造函数,这里是Box。