用lang里面的,要重写好多方法。直接在下面重写类算了
应该是这样实现吧。。
package cn.tedu.basic;
public class Demo_FindMax {
/** * Return max item in arr * Precondition:arr.length>0 * @param arr * @return */
public static Comparable findMax(Comparable[] arr) {
int maxIndex = 0 ;
for(int i=1 ; i<arr.length ; i++) {
if(arr[i].compareTo(arr[maxIndex])>0) {
maxIndex = i ;
}
}
return arr[maxIndex] ;
}
/** * Test findMax on Shape and String objects. * @param args */
public static void main(String[] args) {
String[] st1 = {
"123",
"222",
"a33",
"1"
};
System.out.println(findMax(st1));
Shape[] sh1 = {
new Circle(2.0),
new Shape(3.0),
new Rectangle(3.0 , 4.0)
};
System.out.println(findMax(sh1).getClass());
}
}
class Shape implements Comparable{ //形状
double area;
public Shape(double area){
this.area = area;
}
@Override
public int compareTo(Object o) {
Shape anotherShape = (Shape) o;
return (int) (this.area - anotherShape.area);
}
}
class Circle extends Shape{ //圆形
public Circle(double r) {
super(r*r*Math.PI);
}
}
class Rectangle extends Shape{//矩形
public Rectangle(double l , double w ) {
super(l*w);
// TODO Auto-generated constructor stub
}
}