[英]equals() and hashcode() methods in java
來自同一作業的另一個問題:
//attributes:
public int identifikace;
public String druh;
public int vek;
public Majitel majitel;
//constructor:
public Zvire(int identifikace, String druh, int vek, Majitel majitel){
this.identifikace = identifikace;
this.druh = druh;
this.vek = vek;
this.majitel = majitel;
一些簡單的 getter 等。現在我應該做的是:“到類 Zvire,添加方法 equals() 和 hashcode() 實現,因此如果兩個Zvire具有相同的identifikace屬性,則它們是相同的。” (不是理想的翻譯,但您可能明白這一點)
我可能會弄明白,但我開始着急了。 有人會介意非常簡單地經歷它嗎?
非常感謝!
讓你的 IDE 為你做這件事,這是你從 IntelliJ 得到的:
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) return false;
final Zvire zvire = (Zvire) o;
return this.identifikace == zvire.identifikace;
}
@Override
public int hashCode() {
return Objects.hash(this.identifikace);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.