【发布时间】:2012-05-02 04:10:41
【问题描述】:
我被告知 compareTo 必须返回一个 int...而不是 Boolean。
例如:
返回
0 如果 b 相等
-1 如果 a
+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();
【问题讨论】:
标签: java