3

我在课堂上有一个项目作业。我已经了解了重载的基础知识,但在某一点上我完全糊涂了。如何仅从我尝试使用的方法输出?好吧,让我向您展示代码而不是解释。

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 宽度创建的矩形”。我不确定接下来要添加什么来实现这一点。任何帮助将不胜感激。

4

8 回答 8

9

我猜

  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)。

于 2011-01-12T19:59:23.863 回答
4
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);
于 2011-01-12T19:59:49.277 回答
3

所以你想要这个:

Box BoxObject1 = new Box(1);
Box BoxObject2 = new Box(1,2);
Box BoxObject3 = new Box(1,2,3);
于 2011-01-12T20:00:34.527 回答
2

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

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

        new Box(1);

这将调用带有签名的构造函数public Box(int length)

于 2011-01-12T19:59:44.353 回答
2

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

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.
于 2011-01-12T20:13:07.727 回答
1

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

于 2011-01-12T20:06:03.907 回答
0

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

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

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);
于 2011-01-12T21:13:54.773 回答
0

你应该考虑这样的事情。它干净了很多。一种构造函数是“通用构造函数”。如果您更改内部实现细节,您将更改这个构造函数。

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

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:~$ 
于 2011-01-12T22:19:45.670 回答