如何处理多个构造函数?
How to handle multiple constructors?
你好,我在赋值方面遇到问题,我需要在一个 class 中创建 3 个构造函数。为矩形中的 2 个角初始化 2 个坐标。错误 Eclipse 给出:"Duplicate method" 和 "Multiple markers on this line" 构造错误。
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)
}
// ...
}
您还可以创建一个 class 来表示一个 "point"(具有 X 值和 Y 值的坐标)并在您的矩形 class 中使用它:
// 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;
}
换句话说:避免重复代码。您可以将真正的 "assignment" 工作委派给您的最后一个 ctor。然后您想使用 易于阅读 但仍然有意义的名称。在考虑坐标时,x1/y1 比您的方法更容易理解。
你好,我在赋值方面遇到问题,我需要在一个 class 中创建 3 个构造函数。为矩形中的 2 个角初始化 2 个坐标。错误 Eclipse 给出:"Duplicate method" 和 "Multiple markers on this line" 构造错误。
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)
}
// ...
}
您还可以创建一个 class 来表示一个 "point"(具有 X 值和 Y 值的坐标)并在您的矩形 class 中使用它:
// 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;
}
换句话说:避免重复代码。您可以将真正的 "assignment" 工作委派给您的最后一个 ctor。然后您想使用 易于阅读 但仍然有意义的名称。在考虑坐标时,x1/y1 比您的方法更容易理解。