?????Java?б???equals?????????????壨???
???????????? ???????[ 2012/9/17 10:13:11 ] ????????
????????????????????????????????????????????????
Point p1 = new Point(1?? 2); Point q = new Point(2?? 3); System.out.println(p1.equals(p2)); // prints true System.out.println(p1.equals(q)); // prints false |
???????????????????????Point?????????????????????????????
import java.util.HashSet;
HashSet System.out.println(coll.contains(p2)); // prints false |
???????coll????а???p2?????????p1?????????????棬p1??p2???????????????????????У??????????????е??Щ???????p2a????????p2?????????p2a????????Object????Point?????
??????????????????????????????????p2????p2a??????????????μ?????
???????????????????????????????????????equals?汾????и???Object???equals???????????????????????????Object??equals?????????
???????Point???е?equals????????????Point?????Object????????????????????и???Object?е?equals?????????????仯??????????Java????????????????????????????????????????????????????????Point??Point??equals?????????á???????????????????Object???Object???equals?????á???????????????б???????????????????????????????????????p1??p2a?????????x??y???”p1.equals(p2a)”?????????false??????????HasSet??contains????????false?????????????????????????????????????????Object??equals????????Point????仯??????????equals
????????????????????equals???????????£?
????????equals????????????????????????Object??????????????????????????????????????????instanceof?????????????????????????????????????????Point??????????????????????????????????????false??
Object p2a = p2;
System.out.println(p1.equals(p2a)); // prints false
public boolean equals(Object other)
// A better definition?? but still not perfect
@Override public boolean equals(Object other) {
boolean result = false;
if (other instanceof Point) {
Point that = (Point) other;
result = (this.getX() == that.getX() && this.getY() == that.getY());
}
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