运算符 && 不能应用于 'boolean', 'int
Operator && cannot be applied to 'boolean', 'int
我正在尝试重写 equals 方法,但遇到“运算符 && 无法应用于 'boolean','int'”错误,这似乎是由最后一行引起的。我相信 pages == otherEntry.pages
returns 是一个整数,但为什么呢?
@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;
}
Float.compare(otherEntry.rating, rating)
return 一个 int
.
如果要根据Float.compare()
定义的顺序判断两个浮点数是否相等,则改为Float.compare(otherEntry.rating, rating) == 0
。
return 语句将变为
return title.equals(otherEntry.title) &&
Arrays.equals(authors, otherEntry.authors) &&
Float.compare(otherEntry.rating, rating) == 0 &&
ISBN.equals(otherEntry.ISBN) &&
pages == otherEntry.pages;
我正在尝试重写 equals 方法,但遇到“运算符 && 无法应用于 'boolean','int'”错误,这似乎是由最后一行引起的。我相信 pages == otherEntry.pages
returns 是一个整数,但为什么呢?
@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;
}
Float.compare(otherEntry.rating, rating)
return 一个 int
.
如果要根据Float.compare()
定义的顺序判断两个浮点数是否相等,则改为Float.compare(otherEntry.rating, rating) == 0
。
return 语句将变为
return title.equals(otherEntry.title) &&
Arrays.equals(authors, otherEntry.authors) &&
Float.compare(otherEntry.rating, rating) == 0 &&
ISBN.equals(otherEntry.ISBN) &&
pages == otherEntry.pages;