public abstract class Main implements Comparable {
public static void main(String[] args) {
Integer[] intArray = {1,2,3,4,5,6,7,8,9,10};
String[] stringArray = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
java.util.Date[] dateArray = {};
for (int j = 0; j < 10 ; j++)
dateArray[j] = new java.util.Date();
/* Code to call max method and display the largest value of these */
}
public static Object max (Comparable[] a){
Object tempObj = new Object();
for (int i = 0; i < a.length - 1; i++){
if ((a[i]).compareTo(a[i+1]) > 0 )
tempObj = a[i];
else
tempObj = a[i+1];
}
return tempObj;
}
public int compareTo(Object o) {
if (/*this*/ > o)
return 1;
else if (/*this*/ < o)
return -1;
else
return 0;
}
}
虽然以通用的 max(a, b) 格式编写它可能更容易,但其中一个要求是以这种方式编写。我找不到引用实际调用 compareTo 方法的对象值的方法。