?????Java?б???equals?????????????壨?£?
???????????? ???????[ 2012/9/18 10:13:57 ] ????????
???????????????????p????????????????????????????????p????????????????????????
Iterator<Point> it = coll.iterator();
boolean containedP = false;
while (it.hasNext()) {
Point nextP = it.next();
if (nextP.equals(p)) {
containedP = true;
break;
}
}
System.out.println(containedP); // ??? true
???????????????в?????p??????p??????????У??????????????????????е?????ж?????x??????????????p????hashCode???????coll?????????С??????????????????????????????????仰???p????????coll?????????Χ???????????????coll??????
???????????????????????????equals??hashCode???????仯?????????????????????????????????????????????У????????С???????????Щ??Щ???????????????????????С???塣???????????????????????б??????????ò?????????equals?????????????????????????equals???????????Point???????壬?????????hashCode?????????????????????????equalsContents?????????????equals??????????Point?????????????equals??hashCode???????????????????x???p??????????????????????????λ?á?
????????4??????????????equals??????
????Object?е?equals??淶??????equals??????????????null?????????????
?????? ???????????κη?null?X??????x.equals(x)?????true??
?????? ???????????κη???x??y????????????y.equals(x)?????????x.equals(y)?????檔
?????? ????????????κη???x??y????z?????x.equals(y)?????棬??y.equals(z)??????棬???x.equals(z)???÷????檔
?????? ????????????x??y????ε???x.equals(y)?????μ?????????????equals??????????????????e?????????????
?????? ?????κη???x??x.equals(null)????????false.
????Point???equals???????????????????????equals?淶????塣??????????????е???????????÷??????????????????????Point??????ColoredPoint??????Point?????????????????Color??color??????Color??????????????????
public enum Color {
RED?? ORANGE?? YELLOW?? GREEN?? BLUE?? INDIGO?? VIOLET;
}
????ColoredPoint??????equals??????????????????color?????????£?
public class ColoredPoint extends Point { // Problem: equals not symmetric
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 = (this.color.equals(that.color) && super.equals(that));
}
return result;
}
}
??????
???·???
??????????????????
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