我写了这个方法。我不明白为什么它写出这个例外:
Exception in thread "main" java.lang.ClassCastException: Kocsma.Sör cannot be cast to java.lang.Comparable
有人知道我的错误是什么?
编译器引用这一行
beers.put(beer, dl);
这是我的代码:
private Map beers = new TreeMap();
public void Upload(Beer beer, int dl) {
int d = 0;
Beer s = null;
for (Map.Entry item : beers.entrySet()) {
if (item.getKey().equals(beer)) {
d = item.getValue();
s = item.getKey();
}
}
if (s != null) {
beers.put(s, d + dl);
}else
beers.put(beer, dl); // Here is the problem by the Compiler
}Kocsma类:
public Kocsma() {
Upload(new Beer("Borsodi sör", 160, 4.6), 1000);
Upload(new Beer("Pilsner Urquell", 250, 4.4), 800);
Upload(new Beer("Soproni Ászok", 150, 4.5), 900);
Upload(new Beer("Dreher Classic", 200, 5.2), 600);
}