1. 程式人生 > >泛型(二)

泛型(二)

spa ring private 結果 最大值 swa () integer length

  上一次我分享了使用繼承來實現泛型,今天講一下使用接口類型表示泛型。

只有在使用Object已有的那些方法能夠表示所執行的操作時,才能使用Object表示泛型,例如要比較一些圖形的面積大小時,用Object無法實現這個功能,這時我們可以寫一個Shape類

實現Comparable接口,通過重寫compareTo方法來實現這個功能。

public class Shape implements Comparable<Shape >{

    private Integer proportion;

    //含參構造方法
    public Shape(Integer proportion) {
        
super(); this.proportion = proportion; } public Shape() { super(); } //重寫compareTo方法,接收傳入參數,比較proportion大小,返回數值 public int compareTo(Shape s) { if(this.proportion == null || s.proportion == null){ throw new RuntimeException("空指針異常"); }else
if(this.proportion>s.proportion) return 1; else if(this.proportion == s.proportion) return 0; else if(this.proportion < s.proportion) return -1; return 0; } }

再創建三個類,分別是Circle,Square和Rectangle,這三個類都繼承了Shape,並且初始化proportion,調用Shape的有參構造方法,將proportion傳入Shape中,再通過compareTo

方法得到最後的結果。

public class Square extends Shape{

    private Integer proportion;

    public Square(Integer c) {
        super(c*c);
        this.proportion = c*c;
    }
    
    
    @Override
    public String toString() {
        return "正方形 [proportion=" + proportion + "]";
    }
    
}
public class Circle extends Shape{

    private Integer proportion;

    public Circle(Integer r) {
        super(3 *r*r);
        this.proportion = 3 *r*r;
    }

    @Override
    public String toString() {
        return "圓形 [proportion=" + proportion + "]";
    }
    
}
public class Rectangle extends Shape{

    private Integer proportion;

    public Rectangle(Integer c,Integer w) {
        super(c*w);
        this.proportion = c*w;
    }

    @Override
    public String toString() {
        return "長方形 [proportion=" + proportion + "]";
    }
    
}

將所有圖形類型放入Shape數組中比較它們的大小,這時需要一個尋找最大元素的方法FindMaxDemo並測試

public class FindMaxDemo {

    public static Shape findMax(Shape[] s){
        int maxIndex = 0;
        if(s == null)
            return null;
        for(int index=1;index<s.length;index++){
            if(s[index].compareTo(s[maxIndex])>0)
                maxIndex = index;    
        }
        return s[maxIndex];
    }
    
    public static void main(String[] args){
        Shape[] s = new Shape[]{new Circle(3),new Square(4),new Rectangle(3, 4)};
        System.out.println(findMax(s));
    }
    
}

以上我們已經實現了比較圖形大小的功能,但實際上我們所用原理還是上回講的通過繼承實現泛型,只是這次繼承的不是Object,而是擁有compareTo方法的Shape,而我們還可以

將泛型寫的更徹底,這時我們就要使用Comparable接口來接收傳入的參數

public class FindMaxDemo2 {

    @SuppressWarnings("unchecked")
    public static <T> Comparable<T> findMax(Comparable<T>[] c){
        int maxIndex = 0;
        if(c == null)
            return null;
        for(int index=1;index<c.length;index++){
            if(c[index].compareTo((T)c[maxIndex])>0)
                maxIndex = index;    
        }
        return c[maxIndex];
    }
    
    public static void main(String[] args){
        Shape[] s = new Shape[]{new Circle(3),new Square(4),new Rectangle(3,5)};
        String[] ss = new String[]{"circle","square","rectangle"};
        System.out.println(findMax(s));
        System.out.println(findMax(ss));
    }
}

這時我們發現我們不僅可以找出圖形面積的最大值,還可以作其他類型的比較,例如String類型,通過ASCII碼來比較

泛型(二)