我试图覆盖equals方法,但是遇到一个'Operator &&无法应用于'boolean','int'错误,这似乎是由最后一行引起的。我相信pages == otherEntry.pages
返回一个int,但是为什么呢?
@Override
public boolean equals (Object that) {
if (this == that) return true;
if (!(that instanceof BookEntry)) return false;
BookEntry otherEntry = (BookEntry) that;
return title.equals(otherEntry.title) &&
Arrays.equals(authors, otherEntry.authors) &&
Float.compare(otherEntry.rating, rating) &&
ISBN.equals(otherEntry.ISBN) &&
pages == otherEntry.pages;
}
答案 0 :(得分:2)
Float.compare(otherEntry.rating, rating)
返回一个int
。
如果要根据Float.compare(otherEntry.rating, rating) == 0
定义的顺序确定两个浮点数是否相等,请将其更改为Float.compare()
。
return语句将变为
return title.equals(otherEntry.title) &&
Arrays.equals(authors, otherEntry.authors) &&
Float.compare(otherEntry.rating, rating) == 0 &&
ISBN.equals(otherEntry.ISBN) &&
pages == otherEntry.pages;