này được thực hiện như sau (jdk1.6.0_31):Java: về thực hiện bộ sưu tập ReverseComparator
private static class ReverseComparator<T>
implements Comparator<Comparable<Object>>, Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 7207038068494060240L;
public int compare(Comparable<Object> c1, Comparable<Object> c2) {
return c2.compareTo(c1);
}
private Object readResolve() { return reverseOrder(); }
}
Tại sao không thể thay nó được thực hiện như sau:
private static class ReverseComparator<T extends Comparable<T>>
implements Comparator<T>, Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 7207038068494060240L;
public int compare(T c1, T c2){
return c2.compareTo(c1);
}
...
}
Là nó chỉ phong cách hoặc có lý do nào sâu hơn không?
EDIT: mã nguồn được hiển thị là từ Sun/Oracle jdk ((jdk1.6.0_31)).
Mã này ở đâu chính xác? Tôi không tìm thấy nó trong JDK, nhưng có rất nhiều nơi khác. Không có gì cho đến nay thậm chí sử dụng Generics. –
Tôi tìm thấy nó trong jdk1.6.0_31, src.zip. – shrini1000
Nhưng bạn có thể cho tôi biết chính xác địa điểm không? Bên trong lớp nào? –