문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
java:equals_hashcode [2017/07/04 00:57] kwon37xi [동치(equivalence)를 위반해서는 안된다] |
java:equals_hashcode [2017/07/04 01:06] (현재) kwon37xi [동치 문제의 최종 해결책 canEqual] |
||
---|---|---|---|
줄 1: | 줄 1: | ||
====== Java equals & hashCode ====== | ====== Java equals & hashCode ====== | ||
- | * [[java: | + | * [[java: |
* [[java: | * [[java: | ||
줄 33: | 줄 33: | ||
* 반사성(reflexive) : null이 아닌 x에 대해 '' | * 반사성(reflexive) : null이 아닌 x에 대해 '' | ||
- | * 대칭성(symmetric) : null이 아닌 x, y에 대해, 오직 '' | + | * 대칭성(symmetric) : null이 아닌 x, y에 대해, 오직 '' |
- | * 이행성(transitive) : null이 아닌 x,y,z에 대해, '' | + | * 이행성(transitive) : null이 아닌 x,y,z에 대해, '' |
* 일관성(consistent) : null이 아닌 x, y에 대해, 객체의 동등성 비교에 사용된 정보에 변경이 없다면 '' | * 일관성(consistent) : null이 아닌 x, y에 대해, 객체의 동등성 비교에 사용된 정보에 변경이 없다면 '' | ||
* null이 아닌 값 x 에 대해 '' | * null이 아닌 값 x 에 대해 '' | ||
+ | * [[https:// | ||
상속 관계에서는 위 동치성을 쉽게 위반하게 된다. '' | 상속 관계에서는 위 동치성을 쉽게 위반하게 된다. '' | ||
줄 58: | 줄 59: | ||
if (other instanceof Point) { | if (other instanceof Point) { | ||
Point that = (Point) other; | Point that = (Point) other; | ||
- | // that.canEqual(this) 가 핵심이다. 이를 거꾸로 | + | // that.canEqual(this) 가 핵심이다. 이를 거꾸로 |
result = (that.canEqual(this) && this.getX() == that.getX() && this.getY() == that.getY()); | result = (that.canEqual(this) && this.getX() == that.getX() && this.getY() == that.getY()); | ||
} | } | ||
줄 76: | 줄 77: | ||
// that.canEqual(this) 호출을 통해서 상위클래스(Point)는 결코 ColoredPoint와 같을 수 없게 보장됨. | // that.canEqual(this) 호출을 통해서 상위클래스(Point)는 결코 ColoredPoint와 같을 수 없게 보장됨. | ||
- | @Override public boolean canEqual(Object other) { | + | @Override |
+ | public boolean canEqual(Object other) { | ||
return (other instanceof ColoredPoint); | return (other instanceof ColoredPoint); | ||
} | } | ||
</ | </ | ||
+ | * 프로그래머는 상위클래스에 '' | ||
===== 다른 타입간의 equals 탐지 ===== | ===== 다른 타입간의 equals 탐지 ===== | ||
* 서로 다른 타입간의 equals는 항상 '' | * 서로 다른 타입간의 equals는 항상 '' | ||
* [[java: | * [[java: | ||
* [[http:// | * [[http:// |