?????Java?б???equals?????????????壨?£?
???????????? ???????[ 2012/9/18 10:13:57 ] ????????
??????????????°汾??Point???ColoredPoint???屣??????淶??????????????????????Point??ColoredPoint?????????false???????p???????cp??“p.equals(cp)?????????????????cp.canEqual(p)?????false?????????cp.equals(p)????????false??????p???????ColoredPoint????????ColoredPoint??equals?????????????instanceof?????????
??????????????棬?????Point????????????????????????????????????????????????????????????????壬p??pAnon??????????true?????????Щ?????
Point p = new Point(1?? 2);
ColoredPoint cp = new ColoredPoint(1?? 2?? Color.INDIGO);
Point pAnon = new Point(1?? 1) {
@Override public int getY() {
return 2;
}
};
Set<Point> coll = new java.util.HashSet<Point>();
coll.add(p);
System.out.println(coll.contains(p)); // ??? true
System.out.println(coll.contains(cp)); // ??? false
System.out.println(coll.contains(pAnon)); // ??? true
??????Щ??????????????????equals???????岢??????canEquals????????????????????????????????????????????????????б???????ColoredPoint?????????”???????????????????????????????????????” canEqual???????????????????????pAnon????????????????????canEqual?????????????????Point????????ж???
????canEqual????????????????????????Υ????Liskov?滻???(LSP)?????磬??????????????????????????(??????? canEqual?????汾?????.getClass()??????汾)???????2???????????????????????????????丸????б??????Υ????LSP???????????LSP???????????????κ?????????????????????????????滻?????????????У????cp??x??y?????????Щ??????е?????”coll.contains(cp)”???????false???????????Υ??????LSP???????????????????Point??????????ColoredPointed??????????????????????????????LSP???????????????????????£???????????????????????????淶??
???????????????????????дequals????(??????? canEqual?????汾?????.getClass()??????汾)??????????Υ??LSP????????????????????????????????????????????????????ж????????????磬?????????????????????????????”coll.contains(pAnon)”??????false???????????????????????????????“coll.contains(cp)”????false??????????ColoredPoint???????equals????????????????????????1??2???????????????1??2????????????????????????????????У??????????Point???????????????????????contains?????????????????????????????????????????????????
????–?????–
??????
???·???
??????????????????
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