?????Java?б???equals?????????????壨???
???????????? ???????[ 2012/9/17 10:13:11 ] ????????
?????????:?????????Java?????Object??equals?????????????????????????????????????????????????????????Java????????????????????????????Java????????????????????????ж????????????????????С?
??????
????????????????equals???????????????????????????????????????????????equal???????????
???????Effective Java?????8???У?Josh Bloch???????????????????????????е?????????????????????????equal??????????????????????Bloch????д????
????????????????????????????????????????????????????????????????????????????????equal??????????????
???????Programming in Scala???е??28????????????????????????????????????????????μ???????equal??????????????????????????????????????????Scala?????У???????????????????????Java????????С???????е???????????Programming in Scala?е?????????????????????scala???????Java
???????????????????
????java.lang.Object ??????equals????????????????????????????????????????????????????????д???????equals???????????????????????о????????Java?????2007 paper??????ó??????μ?????????
???????????е?equals?????????????????
??????????????????????????????????????????????????????????????C????????????????ζ????????????????C????????????????????С???????????????????elem1??elem2???????????C?????????????????????elem1.equals(elm2)????ture????????????equals????????????????????п?????????μ??Щ?????
Set hashSet hashSet.add(elem1); hashSet.contains(elem2); // returns false! |
??????equals?????????????4????????equals???????μ???????壺
????1????????????equals?????????signature??Defining equals with the wrong signature.
????2????????equals????????????hashCode???????Changing equals without also changing hashCode.
????3?????????仯???????equals???塣Defining equals in terms of mutable fields.
????4??????????????equals??????Failing to define equals as an equivalence relation.
????????μ?????????????????????4?????塣
????????1?????????equals?????????signature??
????????????????????Point?????????????????
public class Point {
private final int x;
public Point(int x?? int y) {
public int getX() {
public int getY() {
// ... |
????????????????????????????????????equals???????
// An utterly wrong definition of equals public boolean equals(Point other) { return (this.getX() == other.getX() && this.getY() == other.getY()); } |
??????
???·???
??????????????????
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