为什么Comparable使用Java ?为什么有人会Comparable在课堂上实施?您需要实施可比较的现实生活示例是什么?
不断收到错误消息,但不知道为什么。无法获取代码以使用Collections.sort()对列表进行排序
这就是我所拥有的。3个Java文件。
接口文件。
public interface Comparable<T> {
public int compareTo(T other);
}
Run Code Online (Sandbox Code Playgroud)
类文件。
public class Date implements Comparable<Date>{
private int year;
private int month;
private int day;
public Date(int month, int day, int year){
this.month = month;
this.day = day;
this.year = year;
}
public int getYear(){
return this.year;
}
public int getMonth(){
return this.month;
}
public int getDay(){
return this.day;
}
public String toString(){
return month + "/" + day + "/" + year;
}
public int compareTo(Date other){ …Run Code Online (Sandbox Code Playgroud)