?????Java?б???equals?????????????壨?£?
???????????? ???????[ 2012/9/18 10:13:57 ] ????????
????canEqual ????
????????????????????????????譚???????????????????????????????????в???????????????????????淶????????????????????????????????????????????????equals??hashCode?????????????????????????·????????equals(??hashCode)???????????????????????????????????????????????????????????????????????????????????????????????equals???????????????canEqual????????????????????????
public boolean canEqual(Object other)
???????other ??????canEquals?????????????????????????????????÷????棬??????false???????????equals????????????????????????????????????????Point????μ???????????
public class Point {
private final int x;
private final int y;
public Point(int x?? int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override public boolean equals(Object other) {
boolean result = false;
if (other instanceof Point) {
Point that = (Point) other;
result =(that.canEqual(this) && this.getX() == that.getX() && this.getY() == that.getY());
}
return result;
}
@Override public int hashCode() {
return (41 * (41 + getX()) + getY());
}
public boolean canEqual(Object other) {
return (other instanceof Point);
}
}
????????汾??Point???equals?????а????????????????????canEquals???????????????????????????????????????????Point?е?canEqual?????????е?Point??????????????
??????????ColoredPoint????????
public class ColoredPoint extends Point { // ????Υ???????
private final Color color;
public ColoredPoint(int x?? int y?? Color color) {
super(x?? y);
this.color = color;
}
@Override public boolean equals(Object other) {
boolean result = false;
if (other instanceof ColoredPoint) {
ColoredPoint that = (ColoredPoint) other;
result = (that.canEqual(this) && this.color.equals(that.color) && super.equals(that));
}
return result;
}
@Override public int hashCode() {
return (41 * super.hashCode() + color.hashCode());
}
@Override public boolean canEqual(Object other) {
return (other instanceof ColoredPoint);
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11