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

时间:2011-01-12 19:56:53

标签: java constructor-overloading

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

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

8 个答案:

答案 0 :(得分: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)。

答案 1 :(得分: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);

答案 2 :(得分:3)

所以你想要这个:

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

答案 3 :(得分:2)

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

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

        new Box(1);

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

答案 4 :(得分: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.

答案 5 :(得分:1)

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

答案 6 :(得分: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);

答案 7 :(得分: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:~$