泛型(Generics Types)學習筆記

Generics Types 泛型學習筆記 1

作者:冰雲 icecloud(AT)sina.com
BLOG: http://icecloud.51.net

時間:2004.02.15

 

版權聲明:

本文由冰雲完成,首發於CSDN,作者保留中文版權。
未經許可,不得使用於任何商業用途。
歡迎轉載,但請保持文章及版權聲明完整。
如需聯絡請發郵件:icecloud(AT)sina.com

Java 1.5 提供了泛型支持。前幾天,Sun發佈了一篇tutorial。以下是對該tutorial的學習筆記。

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

Generics in the Java Programming Language

Gilad Bracha

Febrary 16, 2004.

 

1 泛型編譯後實際上會產生不同的類型聲明

 

 

    public interface List<E> {

       void add(E x);

       Iterator<E> iterator();

    }

    public interface Iterator<E> {

       E next();

       boolean hasNext();

    }

   

 

基本的調用

 

    List<Integer> myIntList = new LinkedList<Integer>();

    myIntList.add(new Integer(0));

    Integer x = myIntList.iterator().next();

 

 

 

Note 1: 每個泛型聲明僅編譯一次,並且爲每個不同類型產生一個單獨的文件。就像舊的類一樣。比如,可能會有List<Integer>類和List<Boolean>類被產生。

原文:A generic type declaration is compiled once and for all, and turned into a single class file, just like an ordinary class or interface declaration.

 

編譯後,相當於產生了下面的類

 

    public interface IntegerList {

       void add(Integer x);

       Iterator<Integer> iterator();

    }

 

 

 

 

2 List<String>不是List<Object>的子類型

 

以下語句將無法通過編譯,出現錯誤在第2行,因爲lo不是ls的父類型。

 

    List<String> ls = new ArrayList<String>();

    List<Object> lo = ls;

   

    lo.add(new Object();

    String s = ls.get(0);

 

 

Note 2: 一般來說,如果FooBar的子類型,G是泛型類型,那麼G<Foo>不是G<Bar>的子類型。書上說這是泛型學習的最大難點。

原文:In general, if Foo is a subtype (subclass or subinterface) of Bar, and G is som generic type declaration, it is not the case that G<Foo> is a subtype of G<Bar>.

 

3泛型的父類型是<?>,通配符類型。

一個用到Collection的泛型方法如下,其中for循環用了新式的方法:

 

    void printCollection(Collection<?> c){

       for (Object e:c)

           System.out.println(e);

    }

 

其中,Collection<?>表示其可以是任何類型。如Collection<Integer>Collection<String>Collection<?>是他們的父類型。

 

Note 3:Collection<?>是所有種類的子類。而不是Collection<Object>。這叫做“wildcard type”通配符類型。

原文:What is the supertype of all kinds of collections? It’s written Collection<?> (pronounced “collection of unknown”), that is, a collection whose element type matches anything. It’s called a wildcard type for obvious reason.

 

4 Bounded wildcards.有限制的通配符類型

 

很不幸,下面的方法調用無法成功。雖然List<Circle>中的每個對象都是Shape類型。

 

    public void drawAll(List<Shape> shapes) {

       for(Shap s:shapes)s.deaw();

    }

    drawAll(List<Circle> circles);

 

 

通過bounded wildcard可以解決:然而這樣做會帶來一點代價。即,它是隻讀的,你不能再向List中添加新元素。

 

 

    public void drawAll(List<? extends Shape> shapes){

       // 下面的調用是錯誤的,只能讀

       shapes.add(0,new Circle());

    }  

 

 

Note 4: <? extends Class>是一種限制通配符類型,它可以接受所有<Class>以及Class的子類型。然而調用代價是,只讀訪問。

原文:We have replaced the type List<Shape> with List<? extends Shape>. Now drawAll() will accept lists of any subclass of Shape. It is an example of a bounded wildcard. It is now illegal to write into shapes in the body of the method.

這是三分之一左右,地鐵上看的,後面的還沒看。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章