我对基于测试创建矩形的 class 有疑问

I have problem with class that creates rectangle based on a test

好的,所以我有一个应该基于以下内容创建的矩形:将创建其中心坐标的方法点以及高度和宽度的值,所有值都基于此测试

public void testRectangle1() {
        Point center = new Point(20, 30);
        Rectangle rect = new Rectangle(center, 20, 20);
        assertAll(
                () -> assertEquals(10, rect.getTopLeft().getX()),
                () -> assertEquals(20, rect.getTopLeft().getY()),
                () -> assertEquals(30, rect.getBottomRight().getX()),
                () -> assertEquals(40, rect.getBottomRight().getY()),
                () -> assertEquals(20, rect.getWidth()),
                () -> assertEquals(20, rect.getHeight())
        );
    }

我已经预先编写了 class 要点,为了整体清晰起见,我将在此处添加

package net.thumbtack.school.figures.v1;

public class Point {

    private int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Point() {
        this(0, 0);
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void moveTo(int newX, int newY) {
        x = newX;
        y = newY;
    }

    public void moveRel(int dx, int dy) {
        x += dx;
        y += dy;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + x;
        result = prime * result + y;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Point other = (Point) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }
}

这是 class 用于创建矩形

package net.thumbtack.school.figures.v1;

public class Rectangle {
    public int width;
    public int height;
    public Point center;
    public int xCenter;
    public int yCenter;
    private Point point;



    public Rectangle(Point center, int width, int height) {
        this.width=width;
        this.height=height;
        this.center=center;
    }


    public Point getTopLeft() {
        Point point = getCenter();
        point.moveRel(- width / 2, - height / 2);
        return point;
    }

    public Point getBottomRight() {
        Point point = getCenter();
        point.moveRel(width / 2,  height / 2);
        return point;
    }

    public int getWidth() {

        return width;
    }


    public int getHeight() {

        return height;
    }

    public Point getCenter() {
       Point center = new Point(xCenter, yCenter);
       return center;
    }

所以问题出在构造函数 public Rectangle(Point center, int width, int height) 中,当我 运行 测试它时 returns wirng 值

预期:<10> 但实际为:<-10> 比较失败: 预期:10 实际:-10

预期:<20> 但实际为:<-10> 比较失败: 预期:20 实际:-10

预计:<30> 但实际为:<10> 比较失败: 预期:30 实际:10

预计:<40> 但实际为:<10> 比较失败: 预期:40 实际:10

我不认为问题出在其他方法中,因为当我在不同但相似的收缩器中使用它们时,一切正常。

正如maloomeister所说,使用前必须初始化xCenteryCenter,否则xCenteryCenter的值为0。

修改getCenter()方法:

public Point getCenter() {
    Point point = new Point(center.getX(), center.getY());
    return point;
}