如何处理多个构造函数?

时间:2017-09-29 11:46:57

标签: java constructor coordinate

您好我的分配问题,我需要在一个类中创建3个构造函数。初始化矩形中2个角的2个坐标。错误 Eclipse 正在给出:"重复方法"和"此行上的多个标记"构造错误。

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

}

3 个答案:

答案 0 :(得分:2)

如评论中所述,您忘记添加参数类型:

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值的坐标)并在Rectangle类中使用它:

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

答案 1 :(得分:1)

您错过了在构造函数参数中指定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;
    }

答案 2 :(得分:0)

因此,正如其他答案所示:您忘记指定参数的类型。

为了完整性:这就是真正写下这个构造函数的方式:

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

换句话说:避免重复代码。您可以将真正的“作业”作业委托给最后一部作品。然后你想使用易于阅读但仍然有意义的名字。在考虑坐标时,x1 / y1例如更容易掌握,然后你的方法。