제목을 쓴 후에도 여전히 Java에서 동등한 버그 교정 구현에 관한 질문을 한 후에이 SO post을 읽었습니다. 이것은 나의 일반적인 구현이다.효율적인 equals (Object o) 구현
@Override
public boolean equals(Object o){
if(o == null) return false;
if(o instanceof CompositePk == false) return false;
if(this == o) return true;
CompositePk that = (CompositePk)o;
return new EqualsBuilder().append(this.id, that.id)
.append(this.bucketId, that.bucketId)
.isEquals();
}
평범한 것을하기 위해 Apache의 EqualsBuilder를 사용한다. 이 내하고, NetBean의 자동 생성 equals(o)
구현
@Override
public boolean equals(Object obj){
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final TemplatesWrapper other = (TemplatesWrapper) obj;
if (this.timeAdded != other.timeAdded && (this.timeAdded == null || !this.timeAdded.equals(other.timeAdded))) {
return false;
}
return true;
}
보다 더 쉬운 나는 2 개 DIFF 프로젝트에서 다음을하지만 그들은 모두 같은 일을하지만 사랑하는 방법을 사용하여 작업을 수행하려고합니다. 어느 스타일을 사용 하시겠습니까? 아니면 어떤 결함이 있습니까? 중간에
public boolean equals(Object ob) {
if (ob == null) return false;
if (ob == this) return true;
if (!(ob instanceof MyClass)) return false; // OR
if (ob.getClass() != getClass()) return false;
// check relevant members
}
두 개의 선이 다른 :
저는 이러한 이유로 Apache Commons EqualsBuilder를 사용하는 것이 좋습니다. 새 필드를 추가 할 때 사용하는 유지 관리가 적습니다. 그러나 필드를 추가 할 때마다 IDEA가 '평등'을 재생성하기 만하면된다고 주장 할 수도 있습니다. 각각 자신에게. :-P –
당신은'||'연산자에 대해 알고 있습니까? ;-) –