我在课堂上有一个项目作业。我已经了解了重载的基础知识,但在某一点上我完全糊涂了。如何仅从我尝试使用的方法输出?好吧,让我向您展示代码而不是解释。
public class Box {
private int length, width, height;
public Box(int length){
this.length=length;
System.out.println("Line created with length of" + length + ".");
}
public Box(int length, int width){
this.length = length;
this.width = width;
System.out.println("Rectangle created with the length of " + length + " ");
System.out.println("and the width of " + width + ".");
}
public Box(int length, int width, int height){
this.length=length;
this.width=width;
this.height=height;
System.out.println("Box created with the length of " + length + ", ");
System.out.println("the width of " + width + ", ");
System.out.println("and the height of " + height +".");
}
}
class BoxTest {
public static void main(String[] args) {
Box BoxObject1 = new Box(1,0,0);
Box BoxObject2 = new Box(1,2,0);
Box BoxObject3 = new Box(1,2,3);
}
}
好的,那么现在!如何调用 BoxTest 类以仅输出给定的内容。例如,使用 Box BoxObject1 我想输出“使用 XX 的长度创建的线”而不是其余的。对于 Box Box Object2,我想输出“使用 XX 长度和 XX 宽度创建的矩形”。我不确定接下来要添加什么来实现这一点。任何帮助将不胜感激。