繁体   English   中英

Java 孩子 class 仅通过父母的属性等于父母

[英]Java child class equal to parent via parent's attributes alone

我有以下父 class:

public class Coordinates {
    private int xCoordinate;
    private int yCoordinate;

    public Coordinates(int xCoordinate, int yCoordinate) {
        this(xCoordinate, yCoordinate, 10, false);
    }

    public Coordinates(int xCoordinate, int yCoordinate, int max) {
        this(xCoordinate, yCoordinate, max, false);
    }

    public Coordinates(int xCoordinate, int yCoordinate, int max, boolean allowedZero) {
        if (allowedZero) {
            if ((xCoordinate >= 0 && yCoordinate >= 0) && (xCoordinate <= max && yCoordinate <= max)) {
                this.xCoordinate = xCoordinate;
                this.yCoordinate = yCoordinate;
            } else {
                throw new IllegalArgumentException(String.format("Either X or Y has set to value <= 0, or > %d", max));
            }
        } else {
            if ((xCoordinate > 0 && yCoordinate > 0) && (xCoordinate <= max && yCoordinate <= max)) {
                this.xCoordinate = xCoordinate;
                this.yCoordinate = yCoordinate;
            } else {
                throw new IllegalArgumentException(String.format("Either X or Y has set to value <= 0, or > %d", max));
            }
        }

    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Coordinates that = (Coordinates) o;
        return xCoordinate == that.xCoordinate &&
                yCoordinate == that.yCoordinate;
    }

    @Override
    public int hashCode() {
        return Objects.hash(xCoordinate, yCoordinate);
    }

    @Override
    public String toString() {
        return String.format("Coordinates (%d, %d)", xCoordinate, yCoordinate);
    }
}

我还创建了以下子 class:

public class Segment extends Coordinates {
    private boolean isDestroyed;

    public Segment(int xCoordinate, int yCoordinate) {
        super(xCoordinate, yCoordinate);
        this.isDestroyed = false;
    }

    public Segment(int xCoordinate, int yCoordinate, int max) {
        super(xCoordinate, yCoordinate, max);
        this.isDestroyed = false;
    }

    public Segment(int xCoordinate, int yCoordinate, int max, boolean allowedZero) {
        super(xCoordinate, yCoordinate, max, allowedZero);
        this.isDestroyed = false;
    }

    public boolean isDestroyed() {
        return isDestroyed;
    }

    public void setDestroyed(boolean destroyed) {
        isDestroyed = destroyed;
    }

    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}

由于“段”只是一个带有一个新字段的“坐标”class,我希望它们可以直接相互比较,仅通过“坐标”类的 X 和 Y 字段。 但是,目前我没有通过 junit4 测试用例,我比较了以下内容:

    @Test
    public void testSegmentToCoordinateComparison() {
        // Given
        Segment segment = new Segment(1, 1);

        // When
        Coordinates coordinates = new Coordinates(1, 1);

        // Then
        Assert.assertEquals(coordinates, segment);
    }

打印的错误如下:

expected: package.Coordinates<Coordinates (1, 1)> but was: package.Segment<Coordinates (1, 1)>

有任何想法吗?

所以你的equals方法是

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Coordinates that = (Coordinates) o;
        return xCoordinate == that.xCoordinate &&
                yCoordinate == that.yCoordinate;
    }

这里的主要违规者是getClass().= o.getClass() ,因为在这种情况下 getClass 是Coordinates而 o.getClass 是Segment 您不想匹配类,只希望 Coordinates 的所有子类使用 Coordinates 进行比较。 因此,请尝试使用 instanceof Coordinates,例如,

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        /* What instanceof is telling us here is that a cast to Coordinates
         * won't generate an error because it's the same class, or a subclass
         * or implementing class of Coordinates.
         */
        if (o instanceof Coordinates) { //evaluates to false if o is null 
            Coordinates that = (Coordinates) o;
            return xCoordinate == that.xCoordinate &&
                    yCoordinate == that.yCoordinate;
        } else {
            return false;
        }
    }

您可以通过覆盖 hashCode 来反映您进行平等比较的意图,这确实有助于我理解您的问题:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM