【发布时间】:2015-06-15 05:08:15
【问题描述】:
所以我正在尝试学习继承类。
首先我创建了一个名为 Box 的类来计算盒子的面积。
然后我创建了一个 TestBox 类,在其中我创建了一个名为 fedEx 的盒子对象。
盒子类:
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类的方法,这个类是用来计算体积的
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。
我可以做些什么来修复我的代码,错误消息是什么意思?
【问题讨论】:
标签: java inheritance compilation extends