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

发布于 2024-10-11 16:42:14 字数 1137 浏览 8 评论 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 = 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 宽度创建的矩形”。我不知道接下来要添加什么才能发生这种情况。任何帮助将不胜感激。

I have a program assignment in class. I already understand the basics of overloading but I am thoroughly confused on one point. How do I output from only the method I am trying to use? Well let me show you the code than explain.

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



 }

}

Ok Now then! How do I call in the class BoxTest to output only what is given. For instance using Box BoxObject1 I want to output "Line created with lenght of XX" not the rest. For Box Box Object2 I want to ouput "Rectangle created with length of XX and width of XX". I am not sure what to add next for this to happen. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

韬韬不绝 2024-10-18 16:42:14

我想

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

I guess

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

is meant to be

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

At the moment, all three of your calls are calling the third constuctor (passing 0 for some of the arguments).

会发光的星星闪亮亮i 2024-10-18 16:42:14
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,0,0);
Box BoxObject2 = new Box(1,2,0);
Box BoxObject3 = new Box(1,2,3);

These are all calling the 3-argument constructor. Perhaps you actually wanted:

Box BoxObject1 = new Box(1);
Box BoxObject2 = new Box(1,2);
Box BoxObject3 = new Box(1,2,3);
把梦留给海 2024-10-18 16:42:14

所以你想要这个:

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

so you want this:

Box BoxObject1 = new Box(1);
Box BoxObject2 = new Box(1,2);
Box BoxObject3 = new Box(1,2,3);
静赏你的温柔 2024-10-18 16:42:14

要调用构造函数,就像调用方法一样,您可以使用构造函数的签名。即:参数的名称、数量和类型。

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

        new Box(1);

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

To invoke a constructor similar to the way you will invoke a method you use the signature of the constructor. That is: Name, Number and Type of parameters.

So to call the first constructor with a single int parameter then you call:

        new Box(1);

which will invoke the constructor with signature public Box(int length).

抱猫软卧 2024-10-18 16:42:14

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

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.

You may also consider constructing your class like so that constructors can leverage each other reducing the amount of code duplication:

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 + ".");
    }

}

Although not appropriate for this use case (as per the reasons given by extraneon) the following is an alternate possibility, useful if you have a variable number of parameters.

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 + ".");
        }
    }

}

Both of these approaches work with the following:

public class Demo {

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

To give you the desired output:

Line created with length of 1.
and the width of 2.
纵山崖 2024-10-18 16:42:14

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

What you are doing is similar to constructor telescoping. This doesn't scale well and has the same disadvantages listed in the link provided. A constructor should be able to create an object that satisfies all the invariants. For the step-wise build of an Object use a Builder.

尝蛊 2024-10-18 16:42:14

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

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

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

As a comment on your question, and not really an answer:

if you have constructors which share part of the setup (as yours do) you can call other constructors from within a constructor:

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 +".");    
 }
}    

In this case the constructors are quite trivial but if you have constructors which contain somewhat more code it will help you prevent code duplication and thus bugfixing (and possibly forgetting to fix one of the constructors). It also highlights the differences between the constructors.

Also consider Pangea's advice on using a builder. It's more readable:

// 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);
灰色世界里的红玫瑰 2024-10-18 16:42:14

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

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

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:~$ 

You should think about something like this. It's a lot cleaner. One constructor is the "general purpose constructor." Were you to change the internal implementation details, you would change this one constructor.

This solution has the virtue of eliminating a lot of duplicate code.

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);
    }
}

Here is the result at the command line.

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:~$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文