java中重载构造方法的例程_Java构造函数使用多种方法重载

我在课堂上有一个程序任务.我已经理解了重载的基础知识,但我对一点非常困惑.如何仅从我尝试使用的方法输出?那么让我告诉你代码而不是解释.

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的矩形”.我不确定接下来要添加什么才能实现.任何帮助将不胜感激.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值