我有trouble comparing objects of another class
,我该怎么办?
我做了一个很好的例子,希望代码应该是不言自明的。
蛋糕.java:
public class Cake implements Comparable<Cake> {
private final int tastyness;
public Cake(tastyness) {
this.tastyness = tastyness;
}
public int compareTo(Cake other) {
return this.tastyness - other.tastyness;
}
}
makeBestDinner.java:
public class makeBestDinner {
List<Cake> cakes = new ArrayList<cake>();
// Make a whole lot of cakes here
if (cakes[0] > cakes[1]) {
System.out.println("The first cake tastes better than the second");
}
// Do the same for beverages
}