我被告知compareTo必須返回一個int ...不是布爾值。
例如:
compareTo:如何返回一個布爾值?
返回
0如果一個等於b
-1,如果一個< b
1如果A> B
我稍微由混淆。任何幫助將不勝感激。
public int compareTo(Cheese anotherCheese)
throws ClassCastException
{
if (!(anotherCheese instanceof Cheese))
throw new ClassCastException("A Cheese object expected.");
if(getCheeseType().compareTo(anotherCheese.getCheeseType()))
return -1;
else if (getCheeseType().compareTo(anotherCheese.getCheeseType()))
return 1;
else
return cheesePrice > anotherCheese.getCheesePrice();
}
當我編譯,我得到的錯誤信息說:
不兼容的類型
if(getCheeseType().compareTo(anotherCheese.getCheeseType()))
不兼容的類型
else if (getCheeseType().compareTo(anotherCheese.getCheeseType()))
不兼容的類型
return cheesePrice > anotherCheese.getCheesePrice();