使用多种方法的 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 宽度创建的矩形”。我不知道接下来要添加什么才能发生这种情况。任何帮助将不胜感激。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我想
这意味着
目前,您的所有三个调用都在调用第三个构造函数(为某些参数传递 0)。
I guess
is meant to be
At the moment, all three of your calls are calling the third constuctor (passing 0 for some of the arguments).
这些都在调用 3 参数构造函数。也许您实际上想要:
These are all calling the 3-argument constructor. Perhaps you actually wanted:
所以你想要这个:
so you want this:
要调用构造函数,就像调用方法一样,您可以使用构造函数的签名。即:参数的名称、数量和类型。
因此,要使用单个 int 参数调用第一个构造函数,请调用:
它将调用带有签名
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:
which will invoke the constructor with signature
public Box(int length)
.您还可以考虑像这样构造您的类,以便构造函数可以相互利用,减少代码重复量:
虽然不适合此用例(根据 extraneon 给出的原因),但以下是另一种可能性,如果您有参数数量可变。
这两种方法都适用于以下情况:
为您提供所需的输出:
You may also consider constructing your class like so that constructors can leverage each other reducing the amount of code duplication:
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.
Both of these approaches work with the following:
To give you the desired output:
您所做的类似于构造函数伸缩。这不能很好地扩展,并且具有所提供的链接中列出的相同缺点。构造函数应该能够创建一个满足所有不变量的对象。对于对象的逐步构建,请使用构建器。
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.
作为对你的问题的评论,而不是真正的答案:
如果你有共享部分设置的构造函数(就像你的那样),你可以从构造函数中调用其他构造函数:
在这种情况下,构造函数非常简单,但如果你有包含更多代码的构造函数将帮助您防止代码重复,从而防止错误修复(并且可能忘记修复其中一个构造函数)。它还强调了构造函数之间的差异。
另请考虑 Pangea 关于使用构建器的建议。它更具可读性:
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:
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:
你应该考虑这样的事情。干净多了。一种构造函数是“通用构造函数”。如果您要更改内部实现细节,则需要更改此构造函数。
该解决方案的优点是消除了大量重复代码。
这是命令行的结果。
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.
Here is the result at the command line.