繁体   English   中英

Java构造函数使用多种方法重载

[英]Java Constructor Overloading using multiple methods

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

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

我猜

  Box BoxObject1 = new Box(1,0,0);
  Box BoxObject2 = new Box(1,2,0);
  Box BoxObject3 = new Box(1,2,3);

本来应该是

  Box BoxObject1 = new Box(1);
  Box BoxObject2 = new Box(1,2);
  Box BoxObject3 = new Box(1,2,3);

此时,所有三个调用都调用第三个构造函数(对于某些参数传递0)。

Box BoxObject1 = new Box(1,0,0);
Box BoxObject2 = new Box(1,2,0);
Box BoxObject3 = new Box(1,2,3);

这些都是调用3参数构造函数。 也许你真的想要:

Box BoxObject1 = new Box(1);
Box BoxObject2 = new Box(1,2);
Box BoxObject3 = new Box(1,2,3);

所以你想要这个:

Box BoxObject1 = new Box(1);
Box BoxObject2 = new Box(1,2);
Box BoxObject3 = new Box(1,2,3);

要调用与调用方法类似的构造函数,请使用构造函数的签名。 即:参数的名称,数量和类型。

因此,要使用单个int参数调用第一个构造函数,然后调用:

        new Box(1);

它将使用签名public Box(int length)调用构造函数。

您也可以考虑构建类,以便构造函数可以相互利用减少代码重复的数量:

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);
        this.width = width;
        System.out.println("and the width of " + width + ".");
    }

    public Box(int length, int width, int height) {
        this(length, width);
        this.height = height;
        System.out.println("and the height of " + height + ".");
    }

}

虽然不适合这种用例(根据extraneon给出的原因),但如果您有可变数量的参数,则可以使用以下替代方法。

public class Box {

    private int length, width, height;

    public Box(int... param) {
        if(param.length > 0) {
            length = param[0];
            System.out.println("Line created with length of " + length + ".");
        }
        if(param.length > 1) {
            width = param[1];
            System.out.println("and the width of " + width + ".");
        }
        if(param.length > 2) {
            height = param[2];
            System.out.println("and the height of " + height + ".");
        }
    }

}

这两种方法都适用于以下方面:

public class Demo {

    public static void main(String[] args) {
        //new Box(1);
        new Box(1,2);
        //new Box(1,2,3);
    }
}

为您提供所需的输出:

Line created with length of 1.
and the width of 2.

你在做什么类似于构造函数伸缩 这不能很好地扩展,并且具有与所提供的链接中列出的相同的缺点。 构造函数应该能够创建满足所有不变量的对象。 对于逐步构建Object,请使用Builder。

作为对您的问题的评论,而不是真正的答案:

如果你有共享部分设置的构造函数(就像你的那样),你可以在构造函数中调用其他构造函数:

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); // calls Box(length)
  this.width = width;
  System.out.println("and the width of " + width + ".");
 }
 public Box(int length, int width, int height){
  this(length, width); // calls Box(length, width) which calls Box(length)
  this.height=height;
  System.out.println("and the height of " + height +".");    
 }
}    

在这种情况下,构造函数是非常简单的,但如果你有包含更多代码的构造函数,它将帮助你防止代码重复,从而修复错误(并可能忘记修复其中一个构造函数)。 它还强调了构造函数之间的差异。

还要考虑Pangea关于使用构建器的建议。 它更具可读性:

// if no parameters are required and all are optional
Box box0 = new Box.Builder().build();
Box box1 = new Box.Builder().length(1).build();
Box box2 = new Box.Builder().length(1).width(2).build();
Box box3 = new Box.Builder().length(1).width(2).height(3).build();

// if length is required and others are optional - that's your Box
Box box1 = new Box.Builder(1).build();
Box box2 = new Box.Builder(1).width(2).build();
Box box3 = new Box.Builder(1).width(2).height(3).build();

// For comparison - your constructors. It's less obvious what is length, 
// width or height
Box box1 = new Box(1);
Box box2 = new Box(1,2);
Box box3 = new Box(1,2,3);

你应该考虑这样的事情。 它更清洁了。 一个构造函数是“通用构造函数”。 如果要更改内部实现细节,可以更改此构造函数。

该解决方案具有消除大量重复代码的优点。

public class Box
{
    private int length, width, height;

    public Box(int length)
    {
        this(length, 0, 0);
    }
    public Box(int length, int width)
    {
        this(length, width, 0);
    }
    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 +".");
    }
    public static void main(String[] args)
    {
        Box BoxObject1 = new Box(1);
        Box BoxObject2 = new Box(1,2);
        Box BoxObject3 = new Box(1,2,3);
    }
}

这是命令行的结果。

morrison@odonata:~$ java Box
Box created with the length of 1, 
the width of 0, 
and the height of 0.
Box created with the length of 1, 
the width of 2, 
and the height of 0.
Box created with the length of 1, 
the width of 2, 
and the height of 3.
morrison@odonata:~$ 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM