Java子类仅通过父的属性等于父类



我有以下父类:

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

我还创建了以下子类:

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

由于"Segment"只是一个带有一个新字段的"Coordinates"类,我希望它们能够通过"Coordinations"类的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的所有子类都使用Coordinate进行比较。所以试着使用instanceofCoordinates,比如

@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来反映你对平等比较的意图,从而获得加分,这确实帮助我理解了你的问题:(

最新更新