Java 如何处理多个构造函数?
您好,我有分配问题,我需要在一个类中创建3个构造函数。为矩形中的两个角点初始化两个坐标。错误Eclipse给出了构造的“复制方法”和“此行上的多个标记”错误Java 如何处理多个构造函数?,java,constructor,coordinate,Java,Constructor,Coordinate,您好,我有分配问题,我需要在一个类中创建3个构造函数。为矩形中的两个角点初始化两个坐标。错误Eclipse给出了构造的“复制方法”和“此行上的多个标记”错误 public class Rectangle { private double lowleftx; private double lowlefty; private double uprightx; private double uprighty; public Rectangle() { this.lowleftx = 0
public class Rectangle {
private double lowleftx;
private double lowlefty;
private double uprightx;
private double uprighty;
public Rectangle() {
this.lowleftx = 0;
this.lowlefty = 0;
this.uprightx = 1;
this.uprighty = 1;
}
public Rectangle(uprightx, uprighty) {
this.lowleftx = 0;
this.lowlefty = 0;
}
public Rectangle(uprightx, uprighty, lowleftx, lowlefty) {
this.lowleftx = lowleftx;
this.lowlefty = lowlefty;
this.uprightx = uprightx;
this.uprighty = uprighty;
}
public double getLowleftx() {
return lowleftx;
}
public void setLowleftx(double lowleftx) {
this.lowleftx = lowleftx;
}
public double getLowlefty() {
return lowlefty;
}
public void setLowlefty(double lowlefty) {
this.lowlefty = lowlefty;
}
public double getUprightx() {
return uprightx;
}
public void setUprightx(double uprightx) {
this.uprightx = uprightx;
}
public double getUprighty() {
return uprighty;
}
public void setUprighty(double uprighty) {
this.uprighty = uprighty;
}
}您没有在构造函数参数中指定
double
数据类型,因此请按如下所示添加它:
public Rectangle() {
this.lowleftx = 0;
this.lowlefty = 0;
this.uprightx = 1;
this.uprighty = 1;
}
public Rectangle(double uprightx, double uprighty) {
this.lowleftx = 0;
this.lowlefty = 0;
}
public Rectangle(double uprightx, double uprighty,
double lowleftx, double lowlefty) {
this.lowleftx = lowleftx;
this.lowlefty = lowlefty;
this.uprightx = uprightx;
this.uprighty = uprighty;
}
如评论中所述,您忘记添加参数的类型:
public Rectangle(double uprightx, double uprighty...)
您可以通过使用其他构造函数的所有参数调用构造函数来优化代码:
public class Rectangle {
private double lowLeftX;
private double lowLeftY;
private double upRightX;
private double upRightY;
public Rectangle(double lowLeftX, double lowLeftY, double upRightX, double upRightY) {
this.lowLeftX = lowLeftX;
this.lowLeftY = lowLeftY;
this.upRightX = upRightX;
this.upRightY = upRightY;
}
public Rectangle(double upRightX, double upRightY) {
this(0, 0, upRightX, upRightY); // = Rectangle(0, 0, upRightX, upRightY)
}
public Rectangle() {
this(0, 0, 1, 1); // = Rectangle(0, 0, 1, 1), or Rectangle(1, 1)
}
// ...
}
还可以创建一个类来表示“点”(具有X值和Y值的坐标),并在矩形类中使用它:
// Point.java
public class Point {
private final double x;
private final double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
// ...
}
// Rectangle.java
public class Rectangle {
private final Point lowLeft;
private final Point upRight;
public Rectangle(final Point lowLeft, final Point upRight) {
this.lowLeft = lowLeft;
this.upRight = upRight;
}
public Rectangle(final Point upRight) {
this(new Point(0, 0), upRight);
}
public Rectangle() {
this(new Point(1, 1));
}
}
因此,正如在其他答案中所看到的:您忘记了为参数指定类型 为了完整性:这就是你应该如何写下这个构造器:
public Rectangle() {
this(1, 1);
}
public Rectangle(double x1, double y1) {
this(0, 0, x1, y1);
}
public Rectangle(double x1, double y1, double x2, double y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
换句话说:避免重复代码。您可以将真正的“任务”工作委托给上一个ctor。然后,您希望使用易于阅读但仍有意义的名称。例如,在考虑坐标时,x1/y1比您的方法更容易掌握。您忘记将参数类型输入构造函数<代码>公共矩形(直立X,直立)。它们应该类似于
公共矩形(double-uprightx,double-uprighty)
。公共矩形(双lowerleft,双upperright){…提示:写下随机代码是学习编程语言的一种低效策略。首先学习书籍/教程,了解正确的语法。而不是写下“某物”然后假设这个社区的存在是为了帮助修复语法错误。一个巨大的错误:Eclipse根本不给你任何东西,它是虚拟机。Eclipse只是另一个IDE。不是downvoter,但我想这种方法是否“更好”取决于谁看它。我也听过很多“不要这样做”-主张辩护。您可能还希望将实际的双精度值传递给下一个构造函数,而不是int。编译器或JVM不会在意,是的,但它更可读,避免了问题,然后有人添加了一个矩形(int,int)
构造函数(无论出于何种原因)。