Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java子类通过父类与父类相等';光是他的属性_Java_Inheritance_Equals - Fatal编程技术网

Java子类通过父类与父类相等';光是他的属性

Java子类通过父类与父类相等';光是他的属性,java,inheritance,equals,Java,Inheritance,Equals,我有以下父类: public class Coordinates { private int xCoordinate; private int yCoordinate; public Coordinates(int xCoordinate, int yCoordinate) { this(xCoordinate, yCoordinate, 10, false); } public Coordinates(int xCoordinate,

我有以下父类:

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);
    }
}
由于“段”只是一个带有一个新字段的“坐标”类,所以我希望它们能够通过“坐标”类的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)>
应为:package.Coordinates,但为:package.Segment

有什么想法吗?

所以你的equals方法是

@覆盖
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
坐标=(坐标)o;
return xCoordinate==that.xCoordinate&&
yCoordinate==that.yCoordinate;
}
这里的主犯是
getClass()!=o、 getClass()
,因为在本例中,getClass是
坐标
,而o.getClass是
。您不希望匹配这些类,只希望坐标的所有子类都使用坐标进行比较。所以试着用instanceof坐标,比如

@覆盖
公共布尔等于(对象o){
如果(this==o)返回true;
/*这里instanceof告诉我们的是坐标的转换
*不会生成错误,因为它是同一个类或子类
*或者实现坐标类。
*/
if(o instanceof Coordinates){//如果o为null,则计算结果为false
坐标=(坐标)o;
return xCoordinate==that.xCoordinate&&
yCoordinate==that.yCoordinate;
}否则{
返回false;
}
}

你可以通过重写哈希代码来获得你的相等性比较的好处,这真的帮助我理解了你的问题:)

最后的想法:考虑添加一个xxRead和你的协调器,使用GETTER而不是直接的字段访问。谢谢,你的解决方案完美无瑕。我有时倾向于阅读自动生成的equals()和hashcode()方法实现。我的错,还有Intellij,因为他这么方便;)
expected: package.Coordinates<Coordinates (1, 1)> but was: package.Segment<Coordinates (1, 1)>