2014-04-13 2 views
0

현재 시험을 위해 개정 중이며 equals 메서드, compareTo 메서드 및 hashCode 메서드를 구현해야하는 질문을받을 가능성이 높습니다. 그것은 개방 된 책입니다. 그러므로 저는이 영역에서 특히 유용 할 수 있다고 생각하기 때문에 이것을 최대한 활용하려고합니다. 예를 들어, 내가 누군가를 elses 코드를 다음과 다음을 조정하여 compareTo 메소드를 구현하는 데 성공, 다른 사람이 = 어쨌든)equals 메서드 구현

, 나는 우리에게 주어진 샘플 시험 문제가되므로 나는 시험이 우위를 원하고 그것을 요청 우리가 정확히 이것. 다른 사람들의 코드를 예제로 사용하면서 equals 메소드를 구현하려고 시도하고 있는데, 이는 허용 된 이후로 템플릿을 표시 할 때 템플릿으로 사용하기 때문입니다. 그러나 equals 메서드는 compareTo와 같은 템플릿을 통해 구현하기가 쉽지 않습니다. 나는 시도해 보았지만 좋지는 않은 것 같아. D 나는 너희들이 내가 잘못 가고있는 곳에서 나를 도울 수 있기를 바랬다. 나는 확신 할 수는 없지만 아마도 If if 진술 일 수 있다고 생각한다. 마지막으로, 위의 방법 중 하나에 대한 유용한 정보가 있다면 오픈 북 시험에서 매우 감사하겠습니다! 내가 equals 메소드를 구현하고있어 여기에이 코드

public class Books { 

    private final String title; 
    private final String author; 
    private final int edition; 

    public Books(String title, String author, int edition) 
    { 
     this.title = title; 
     this.author = author; 
     this.edition = edition; 
    } 

    public String getTitle(){ 
     return title; 
    } 
    public String getAuthor(){ 
     return author; 
    } 
    public int getEdition(){ 
     return edition; 
    } 

: 나는) = 나는 통과 할 수 있습니다 여부를 정말로/걱정 확실 해요으로

어쨌든 나는 다음과 같은 코드를 주어졌다 나는 누군가 elses를 따라 다니는 동안 그것을 한 시도.

public boolean equals(Object obj){ 
    if (this == obj) 
     return true; 
    if (obj == null) 
     return false; 
    if(getClass() != obj.getClass()) 
    return false; 
    Books other = (Books) obj; 
    if (getTitle() == null){ 
     if (other.getTitle() != null) 
      return false; 
    } 

    else if 
     (!getTitle().equals(other.getTitle())) 
    return false; 
    return true; 

    else if (getAuthor() == null){ 
     if (other.getAuthor() != null) 
      return false; 
    } 

    else if 
     (!getAuthor().equals(other.getAuthor())) 
    return false; 
    return true; 

    if (getEdition() != other.getEdition()) 
     return false; 


     } 

나는 그것이 =/

+0

아니다, 방법은 일반적으로 우리가 다음을 수행해야 같습니다 왜 equals를 수동으로 구현할 것인가? 이클립스는 당신에게'Source-> Generate hashCode() and equals() ... '를 시도 할 수있다. –

+0

시험에서 그렇게 할 수 없다. = ( – user3443834

+0

모든 IDE에는 비슷한 가능성이 있기 때문에 이상한 시험 문제가있는 것 같다. 도우미. 내 권장 사항은 일식과 같은 IDE에서 생성 된 같음을 연구하고 개체를 비교하는 방법의 논리적 분석을 이해하는 것입니다. –

답변

0

성장됩니다으로 문 흐름 경우, 내가 잘 통해 따라하기 위해 노력 어떻게 거의 나사 것을 알고이 이클립스 해당 클래스에 대한 생산 것입니다 :

@Override 
public boolean equals(Object obj) { 
    if (this == obj) 
     return true; 
    if (obj == null) 
     return false; 
    if (getClass() != obj.getClass()) 
     return false; 
    Books other = (Books) obj; 
    if (author == null) { 
     if (other.author != null) 
     return false; 
    } else if (!author.equals(other.author)) 
     return false; 
    if (edition != other.edition) 
     return false; 
    if (title == null) { 
     if (other.title != null) 
     return false; 
    } else if (!title.equals(other.title)) 
     return false; 
    return true; 
} 
0

재정의 사용자 개체의 특정 라인에서 내 의견

@Override 
    public boolean equals(Object obj) { 
     //check if object received is not null 
     if (obj == null) { 
      return false; 
     } 
     //check if the object received is of the same class 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final Beta other = (Beta) obj; 
     //compare all properties of the class and return true based on oure requirement 
     if ((this.getSample() == null) && (other.getSample() == null)){ 
      return true; 
     } 
     if ((this.getSample().getId().equals(other.getSample().getId())) && (this.getSample().getName().equals(other.getSample().getName()))) { 
      return true; 
     } 
     //if nothing suits well based on our requirement we can directly send false or handle the responsibility of comparison to super class 
     return super.equals(); 
    }