【永恒的瞬間】
          ?Give me hapy ?

          1           泛型 (Generic)

          1.1          說明

                增強了 java 的類型安全,可以在編譯期間對容器內(nèi)的對象進行類型檢查,在運行期不必進行類型的轉(zhuǎn)換。而在 j2se5 之前必須在運行期動態(tài)進行容器內(nèi)對象的檢查及轉(zhuǎn)換

          減少含糊的容器,可以定義什么類型的數(shù)據(jù)放入容器

          ArrayList<Integer> listOfIntegers; // <TYPE_NAME> is new to the syntax

          Integer integerObject;

          listOfIntegers = new ArrayList<Integer>(); // <TYPE_NAME> is new to the syntax

          listOfIntegers.add(new Integer(10)); // 只能是 Integer 類型

          integerObject = listOfIntegers.get(0); // 取出對象不需要轉(zhuǎn)換

          1.2          用法

          聲明及實例化泛型類:

          HashMap<String,Float> hm = new HashMap<String,Float>();

          // 不能使用原始類型

          GenList<int> nList = new GenList<int>();  // 編譯錯誤

          J2SE 5.0 目前不支持原始類型作為類型參數(shù) (type parameter)

          定義泛型接口:

          public interface GenInterface<T> {

              void func(T t);

          }

          定義泛型類:

           

          public class ArrayList<ItemType> { ... }

          public class GenMap<T, V> { ... }

          1

          public class MyList<Element> extends LinkedList<Element> {

                 public void swap(int i, int j) {

                        Element temp = this.get(i);

                        this.set(i, this.get(j));

                        this.set(j, temp);

                 }

               public static void main(String[] args)  {

                        MyList<String> list = new MyList<String>();

                        list.add("hi");

                        list.add("andy");

                        System.out.println(list.get(0) + " " + list.get(1));

                        list.swap(0,1);

                        System.out.println(list.get(0) + " " + list.get(1));

                 }

          }

          2

          public class GenList <T>{

                 private T[] elements;

                 private int size = 0;

                 private int length = 0;

                 public GenList(int size) {

                        elements = (T[])new Object[size];

                        this.size = size;

                 }

                 public T get(int i) {

                        if (i < length) {

                               return elements[i];

                        }

                        return null;

                 }

             public void add(T e) {

                        if (length < size - 1)

                               elements[length++] = e;

                 }

          }

          泛型方法:
          public class TestGenerics{

                 public <T> String getString(T obj) { // 實現(xiàn)了一個泛型方法

                        return obj.toString();

                 }
                 public static void main(String [] args){

                        TestGenerics t = new TestGenerics();

                        String s = "Hello";

                        Integer i = 100;

                        System.out.println(t.getString(s));

                        System.out.println(t.getString(i));

                        }

          }

          1.3          受限泛型

             受限泛型是指類型參數(shù)的取值范圍是受到限制的 . extends 關(guān)鍵字不僅僅可以用來聲明類的繼承關(guān)系 , 也可以用來聲明類型參數(shù) (type parameter) 的受限關(guān)系 . 例如 , 我們只需要一個存放數(shù)字的列表 , 包括整數(shù) (Long, Integer, Short), 實數(shù) (Double, Float), 不能用來存放其他類型 , 例如字符串 (String), 也就是說 , 要把類型參數(shù) T 的取值泛型限制在 Number 極其子類中 . 在這種情況下 , 我們就可以使用 extends 關(guān)鍵字把類型參數(shù) (type parameter) 限制為數(shù)字

          示例

          public class Limited<T extends Number> {

                 public static void main(String[] args) {

                        Limited<Integer> number;   // 正確

                        Limited<String> str;       // 編譯錯誤

                 }

          }

          1.4          泛型與異常

           類型參數(shù)在 catch 塊中不允許出現(xiàn),但是能用在方法的 throws 之后。例:

          import java.io.*;

          interface Executor<E extends Exception> {

                 void execute() throws E;

          }

           public class GenericExceptionTest {

                 public static void main(String args[]) {

                        try {

                               Executor<IOException> e = new Executor<IOException>() {

                                      public void execute() throws IOException{

                                             // code here that may throw an

                                             // IOException or a subtype of

                                             // IOException

                                      }

                                      };

                               e.execute();

                        } catch(IOException ioe) {

                               System.out.println("IOException: " + ioe);

                               ioe.printStackTrace();

                        }

                 }

          }

          1.5          泛型的通配符 "?"

           "?" 可以用來代替任何類型 , 例如使用通配符來實現(xiàn) print 方法。

          public static void print(GenList<?> list) {})

          1.6          泛型的一些局限型

           不能實例化泛型

           T t = new T(); //error 不能實例化泛型類型的數(shù)組

           T[] ts= new T[10];   // 編譯錯誤

           不能實例化泛型參數(shù)數(shù)

           Pair<String>[] table = new Pair<String>(10); // ERROR

           類的靜態(tài)變量不能聲明為類型參數(shù)類型

           public class GenClass<T> {

                private static T t;    // 編譯錯誤

           }

          泛型類不能繼承自 Throwable 以及其子類

          public GenExpection<T> extends Exception{}    // 編譯錯誤

          不能用于基礎(chǔ)類型int

           Pair<double> //error

           Pair<Double> //right

           2           增強循環(huán) (Enhanced for Loop)

           舊的循環(huán)

          LinkedList list = new LinkedList();             

          list.add("Hi");

          list.add("everyone!");

          list.add("Was");

          list.add("the");

          list.add("pizza");

          list.add("good?");          

          for (int i = 0; i < list.size(); i++)

                 System.out.println((String) list.get(i));

          // 或者用以下循環(huán)

          //for(Iterator iter = list.iterator(); iter.hasNext(); ) {

          //Integer stringObject = (String)iter.next();

          // ... more statements to use stringObject...

          //}

          新的循環(huán)

          LinkedList<String> list = new LinkedList<String>();         

          list.add("Hi");

          list.add("everyone!");

          list.add("Was");

          list.add("the");

          list.add("pizza");

          list.add("good?");          

          for (String s : list)

                 System.out.println(s);

          很清晰、方便,一看便知其用法

          3           可變參數(shù) (Variable Arguments)

           實現(xiàn)了更靈活的方法參數(shù)傳入方式, System.out.printf 是個很好的例子

          用法: void test(Object … args)

           一個很容易理解的例子

          public static int add(int ... args){

                 int total = 0;   

                 for (int i = 0; i < args.length; i++)

                        total += args[i];     

                 return total;

          }

          public static void main(String[] args){

                 int a;

                 a = Varargs.add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

                 System.out.println(a);

          }

          4           自動實現(xiàn)裝箱和解箱操作 (Boxing/Unboxing Conversions)

           說明:實現(xiàn)了基本類型與外覆類之間的隱式轉(zhuǎn)換。基本類型至外覆類的轉(zhuǎn)換稱為裝箱,外覆類至基本類型的轉(zhuǎn)換為解箱。這些類包括

          Primitive Type     Reference Type

           boolean           Boolean

          byte              Byte

          char              Character

          short             Short

          int               Integer

          long              Long

          float              Float

          double            Double

          例如,舊的實現(xiàn)方式

          Integer intObject;

          int intPrimitive;

          ArrayList arrayList = new ArrayList();

          intPrimitive = 11;

          intObject = new Integer(intPrimitive);

          arrayList.put(intObject); // 不能放入 int 類型,只能使 Integer

          新的實現(xiàn)方式

          int intPrimitive;

          ArrayList arrayList = new ArrayList();

          intPrimitive = 11;

          // 在這里 intPrimitive 被自動的轉(zhuǎn)換為 Integer 類型

          arrayList.put(intPrimitive);

          5           靜態(tài)導(dǎo)入 (Static Imports)

           很簡單的東西,看一個例子:

          沒有靜態(tài)導(dǎo)入

          Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));

           有了靜態(tài)導(dǎo)入

           import static java.lang.Math.*;

           sqrt(pow(x, 2) + pow(y, 2));

          其中import static java.lang.Math.*;就是靜態(tài)導(dǎo)入的語法,它的意思是導(dǎo)入Math類中的所有static方法和屬性。這樣我們在使用這些方法和屬性時就不必寫類名。 需要注意的是默認包無法用靜態(tài)導(dǎo)入,另外如果導(dǎo)入的類中有重復(fù)的方法和屬性則需要寫出類名,否則編譯時無法通過。

           6          枚舉類(Enumeration Classes)

            用法:public enum Name {types, ….}

           簡單的例子:

           public enum Colors {Red, Yellow, Blue, Orange, Green, Purple, Brown, Black}

           public static void main(String[] args){

               Colors myColor = Colors.Red;

              System.out.println(myColor);

           } 又一個簡單例子:

          import java.util.*;

          enum OperatingSystems {windows, unix, linux, macintosh}

          public class EnumExample1 {

              public static void main(String args[])  {

                  OperatingSystems os; 
                  os = OperatingSystems.windows;

                  switch(os) {

                      case windows:

                          System.out.println(“You chose Windows!”);

                          break;

                      case unix:

                          System.out.println(“You chose Unix!”);               

           

                                               break;

                      case linux:

                          System.out.println(“You chose Linux!”);

                          break;

                      case macintosh:

                          System.out.println(“You chose Macintosh!”);

                          break;

                      default:

           

                          System.out.println(“I don’t know your OS.”);

                          break; 
                  }

           

              }

           

          應(yīng)運enum簡寫的例子:

           import java.util.*;

           public class EnumTest {

             public static void main(String[] args) {

                 Scanner in = new Scanner(System.in);

                 System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");       String input = in.next().toUpperCase();

                Size size = Enum.valueOf(Size.class, input); 
          System.out.println("size=" + size); 
                System.out.println("abbreviation=" + size.getAbbreviation());

                if (size == Size.EXTRA_LARGE)

                   System.out.println("Good job--you paid attention to the _.");    }

           }

          enum Size

           {

              SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");

             private Size(String abbreviation) { this.abbreviation = abbreviation; }

             public String getAbbreviation() { return abbreviation; }

          private String abbreviation;

          }

          enum 類中擁有方法的一個例子:

           enum ProgramFlags {  

              showErrors(0x01),

               includeFileOutput(0x02),

               useAlternateProcessor(0x04);

               private int bit;

               ProgramFlags(int bitNumber){

                   bit = bitNumber;
               }

                public int getBitNumber()   {

                 return(bit);

               }

           }

           public class EnumBitmapExample { 

              public static void main(String args[])   

                  ProgramFlags flag = ProgramFlags.showErrors;         System.out.println(“Flag selected is: “ +    flag.ordinal() +

           “ which is “ +  flag.name());  }

           }

           7          元數(shù)據(jù)(Meta data)

            http://www-900.ibm.com/developerWorks/cn/java/j-annotate1/

           http://www-900.ibm.com/developerworks/cn/java/j-annotate2.shtml

           8          Building Strings(StringBuilder )
          JDK5.0中引入了StringBuilder類,該類的方法不是同步(synchronized)的,這使得它比StringBuffer更加輕量級和有效。

           9          控制臺輸入(Console Input)

           JDK5.0之前我們只能通過JOptionPane.showInputDialog進行輸入,但在5.0中我們可以通過類Scanner在控制臺進行輸入操作

              例如在1.4中的輸入

              String input = JOptionPane.showInputDialog(prompt);

          int n = Integer.parseInt(input);

          double x = Double.parseDouble(input);  

          s = input;  

          5.0中我們可以  

          Scanner in = new Scanner(System.in);

          System.out.print(prompt);

          int n = in.nextInt();

          double x = in.nextDouble();

          String s = in.nextLine();

           10      Covariant Return Types( 不曉得怎么翻譯)JDK5 之前我們覆蓋一個方法時我們無法改變被方法的返回類型,但在JDK5中我們可以改變它
          例如1.4中我們只能 public Object clone() { ... }
          Employee cloned = (Employee) e.clone();

           但是在5.0中我們可以改變返回類型為Employee

           public Employee clone() { ... }

          ...

          Employee cloned = e.clone();


          紅色斜體字在程式碼實作時須自行替換。橘色 [] 表示非必要可省略。
          請注意,如果你直接拷貝程式碼準(zhǔn)備跑跑看,你必須將全型空白(排版用)轉(zhuǎn)為半形空白。請使用 IDE 工具提供的替代功能。
          *********************************************************

          泛型(Generics)  

          1. Generics 可提供 compile-time type 檢查。
          2. 泛型的使用:
           ◇ 類別 (註:介面泛型類似類別泛型,此處不作介紹)
           •定義泛型 Shy
            A> [class-modifiers] class GenericsName<T1 [, T2, T3, ...]> { ...... }
             泛型由泛型名稱(generic type name)、角括號 "<>"、包含在角括號內(nèi)的型態(tài)參數(shù)(type parameter)組成。
             在角括號 "<>" 內(nèi)的型態(tài)參數(shù)( Tx ) 須為某種參考型態(tài)。

             [範(fàn)例]
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
             class MyHashTable<Key, Value> {  // MyHashtable 稱為 generic type name,Key 及 Value 為 type parameter
                           ......
                           Value put(Key k, Value v) {...}
                           Value get(Key k) {...}
                         }
                         class Demo {
                           public static void main(String[] args) {
                             MyHashTable<Integer, String> h = new MyHashTable<Integer, String>( );
                             h.put(new Integer(0), "value");
                             ......
                           }
                         }
                      

            B> 泛型類別(/介面)可被繼承(/實作),子類別(實作介面)無論是否宣告父類別(介面)的型態(tài)參數(shù)皆會繼承型態(tài)參數(shù),並且可以宣告新的型態(tài)參數(shù)。
             子類別若沒有宣告父類別(/介面)的全部型態(tài)參數(shù),則繼承的型態(tài)參數(shù)會自動變?yōu)?Object 型別。 (註:參考偶像良葛格的筆記) (以上這段請見下面 Duncan版主的更正)
              [範(fàn)例]
          1
          2
          3
          4
          5
          6
              class MyGenericsSuper<T1, T2> {
                            ......
                          }
                          class MyGenericsSub<T1, T2, T3> extends MyGenericsSuper<T1, T2> {
                            ......
                          }
                      


              [範(fàn)例]
          1
          2
              interface MyGenericsI<T1, T2> { /* ...... */  }
                          class MyGenericC<T1, T2> implements MyGenericsI<T1, T2> { /* ...... */ }
                      


           •宣告/建立泛型物件(Instantiating a Generic Type) Shy
            A> 宣告泛型物件
             1> concrete parameterized type
              須使用泛型名稱(generic type name),後面加上角括號"<>",角括號"<>" 內(nèi)須以 class 或 interface 名稱取代泛型宣告的型態(tài)參數(shù)(type parameter)來指定泛型真正的使用型態(tài)。

              [範(fàn)例] concrete parameterized type
          1
          2
          3
          4
          5
              Vector<String>       // Vector<String> 稱為 parameterized types
                          Seq<Seq<A>>
                          Seq<String>.Zipper<Integer>
                          Collection<Integer>
                          Pair<String, String>
                      


              [範(fàn)例]
          1
              LinkedList<LinkedList<String>> texts
                      


             2> wildcard type & bounded wildcard
              a> wildcard type (通配型)
               角括號內(nèi)若使用問號 "?" 而不是 class 或 interface 名稱,表示宣告泛型型別為通用型別,可使用任何的參考型態(tài)。
                "?" 表示未知型態(tài)(unknown type)。
              b> bounded wildcard (有限制的通配型)
               wildcard type 可限制型別的範(fàn)圍(bounds)。
               使用 extends 關(guān)鍵字可指定欲使用的型態(tài)必須是某個型別(介面或類別)或其衍生型別。
               使用 super 關(guān)鍵字指定使用某個型別(介面或類別)及其父型別。
               使用 "&" 符號可作多重限制(Multiple bounds)。

               [範(fàn)例]
          1
               MyGenericsClass<? super Integer> obj;  // 只接受 Integer 及其父類別型態(tài)的物件
                      


               [範(fàn)例] can create a variable of type Matrix<Integer> or Matrix<Float>,
          1
               public class Matrix<V extends Number> { ...... }
                      


               [範(fàn)例]
          1
               class C<T extends Comparable<? super T> & Serializable>
                      


               [範(fàn)例]
          1
          2
          3
          4
          5
          6
          7
          8
               interface ConvertibleTo<A> {
                             A convert();
                           }
                           class ReprChange<A implements ConvertibleTo<B>, B implements ConvertibleTo<A>> {   // error, implements 須改為 extends
                             A a;
                             void set(B x) { a = x.convert(); }
                             B get() { return a.convert(); }
                           }
                      

             3> raw type
              宣告泛型物件只使用泛型名稱(generic type name),則預(yù)設(shè)會使用 Object 型別,使用此類物件須自行轉(zhuǎn)換型態(tài),且編譯器會發(fā)出 warning 訊息。
              [註] 原生類型:raw type,指未使用泛型,如 Collection、ArrayList 等 Java 5 之前的舊形式,或指僅使用泛型名稱(generic type without type arguments)。
                Java Language Specification 第 3 版指出以後的版本可能會禁止使用 raw type。

              [範(fàn)例]
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
              class MyGenerics<T> {
                            T value;
                            void setter(T value) {
                              this.value = value;
                            }
                            T getter( ) {
                              return value;
                            }
                          }
                       
                          class MyGenericsDemo {
                            public static void main(String[] args) {
                              MyGenerics<Integer> myObj1 = new MyGenerics<Integer>( );  // 使用  parameterized type
                              myObj1.setter(new Integer(10));
                              System.out.println(myObj1.getter( )) ;    // 10
                       
                              MyGenerics myObj2 = new MyGenerics( );  // 使用 raw type
                              myObj2.setter((Object)new Integer(10));  // warning: [unchecked] unchecked call to setter(T) as a member of the raw type MyGenerics
                              Integer i = (Integer) myObj2.getter( );
                              // System.out.println(i == myObj1);  // Error: incomparable types: java.lang.Integer and MyGenerics
                              // System.out.println(i == myObj2);  // Error: incomparable types: java.lang.Integer and MyGenerics<java.lang.Integer>
                              System.out.println(myObj2.getClass( ) == myObj1.getClass( ));  // true
                            }
                          }
                      

            B> 建立泛型物件須使用 new 關(guān)鍵字,並指定型態(tài)參數(shù)的值(values of the type parameters)。此種指定值的型態(tài)參數(shù)稱為 parameterized type。

             [範(fàn)例]
          1
             MyGenericClass<? extends java.lang.Number> obj = new MyGenericClass<Integer>( );  // Bounded wildcard
                      


             [範(fàn)例]
          1
          2
          3
          4
             LinkedList<String> myStrings;    // A variable of type LinkedList<String>
                         myStrings = new LinkedList<String>( );
                         或
                         LinkedList<String> myStrings = new LinkedList<String>( );  //define an object when you define the variable
                      


           ◇ methods 使用泛型
            [MethodModifiers] [TypeParameters] ResultType MethodName(...) [throws ....] ;{ ... }

           不論 class(interface) 是否宣告泛型,methods 皆可使用泛型。

           [範(fàn)例]
          1
          2
          3
          4
          5
          6
          7
          8
           // T -- return type, <T>-- 指明 agrument 的傳入型態(tài)
                       public <T>  T  ifThenElse(boolean b, T first, T second) {
                         return b ? first : second;
                       }
                       ......
                       String s = ifThenElse(true, "a", "b");   // a
                       Integer i = ifThenElse(false, new Integer(1), new Integer(2));  //2
                       //String s = ifThenElse(b, "PI", new Float(3.14));  // Error
                      


           [範(fàn)例]
          1
          2
          3
           public static <T, S extends T> void copy (List <T> dest, List <S> src) { ...... } ;
                       或
                       public static <T> void copy (List <T> dest, List <? extends T> src) { ...... } ;
                      


           [範(fàn)例]
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
           public class M {
                        public static <T extends Comparable> T minimum(T a, T b) {
                             if(a.compareTo(  b) <= 0) return a;
                         else return b;
                          }
                         public static void main(String[] args) {
                           Integer b1  = new Integer(2);
                           Integer b2  = new Integer(5);
                           Integer min = minimum(b1, b2);
                           System.out.println("Minimum of (2,5) : " + min);
                         }
                       }
                      


           [範(fàn)例]
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
           public <T extends Animal> void printAnimals(Collection <T> animals) {  // Version 1
                         for (Animal nextAnimal: animals) {
                           System.out.println(nextAnimal);
                         }
                       }
                      ---------------------------------------------------------------------------------------------
                       Version 1 的 method 內(nèi)並未用到 T ,可改成 "wildcard" 型式
                       public void printAnimals(Collection <? extends Animal> animals) {   // Version 2
                         for (Animal nextAnimal: animals) {
                           System.out.println(nextAnimal);
                         }
                       }
                      ---------------------------------------------------------------------------------------------
                       method 內(nèi)使用 Type Parameter
                       public <T extends Animal> void printAnimals(Collection<T> animals) {  // Version 3
                         for (T nextAnimal: animals) {
                           System.out.println(nextAnimal);
                         }
                       }
                      ---------------------------------------------------------------------------------------------
                       以 ? 取代 T 會造成錯誤
                       public void printAnimals(Collection<? extends Animal> animals) {  // Version 4
                         for (? nextAnimal: animals) {  // illegal start of expression
                           System.out.println(nextAnimal);
                         }
                       }
                      


          3. 泛型使用的注意事項 DeadDead
           A> static members 內(nèi)不能使用 type parameters
            [範(fàn)例]
          1
          2
          3
          4
          5
          6
          7
          8
            class C<T> {
                          static void m( ) {
                            T t;  // Error: non-static class T cannot be referenced from a static context
                            }
                            static class D {
                                C<T> t;  // Error: non-static class T cannot be referenced from a static context
                            }
                        }
                      

           B> primitive types 不能使用在泛型
           C> "Naked" type parameters (指不包括角括號的型態(tài)參數(shù),如 C<T>,指的是 T ) 不能做的事:
            不能用在鑄型(casts)、instanceof、或 new 運算上。
            不能使用在定義 class 時的 implements 或 extends 子句內(nèi)
            不能用來建立物件,也不能用作父類型 (class Foo<T> extends T),不能用於類別實字(class literal)。

            [範(fàn)例]
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
            import java.util.Hashtable;
                       
                        interface Registry {
                          public void register(Object o);
                        }
                       
                        class C<T> implements Registry {
                          int counter = 0;
                          Hashtable<Integer, T> values;
                          public C( ) {
                            values = new Hashtable<Integer, T>( );
                          }
                          public void register(Object obj) {
                            values.put(new Integer(counter), (T)obj);  // warning: [unchecked] unchecked cast
                            counter++;
                          }
                        }
                      


            [範(fàn)例]
          1
          2
          3
            Collection<String> cs = new ArrayList<String>;
                        if ( cs instanceof Collection<String> )   {  ....  }   //illegal
                        <T> T badCast(T t, Object o) { return (T)o };  // unchecked warning
                      


           D> 異常型態(tài)(exception type)、列舉(enumeration)或匿名內(nèi)部類(anonymous inner class)不能使用型態(tài)參數(shù)(type paramemter)。
            type variables 雖不允許使用在 catch 子句內(nèi),但可用在 methods 利用 throws 所丟出的例外清單中。

            如果 generic class 為 Throwable 的 subclass(直間或間接),會產(chǎn)生 compile-time error。因為 throw/catch mechanism 只能作用在 non-generic classes 上。

            [範(fàn)例]
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
            import java.io.*;
                        interface MyExecutor<E extends Exception> {
                          void execute() throws E;
                        }
                        public class GenericExceptionTest {
                          public static void main(String args[]) {
                            try {
                              MyExecutor<IOException> e = new MyExecutor<IOException>() {
                                public void execute() throws IOException {
                                  // code here that may throw an IOException or a subtype of  IOException
                                }
                              };
                              e.execute();
                            } catch(IOException ioe) {
                              System.out.println("IOException: " + ioe);
                              ioe.printStackTrace();
                            }
                          }
                        }
                      


           E> 泛型不是 covariant
            如果 MySub 是 MySuper 的子類型,當(dāng) MyG 作泛型類別宣告時,MyG<MySub>不是 G<MySuper>的子型態(tài)(subtype)。
            例 List<Object> 不是 List<String> 的父型態(tài)。

            [範(fàn)例]
          1
          2
          3
          4
          5
            Integer[] intArray = new Integer[10];
                        Number[] numberArray = intArray;  // OK
                        List<Integer> intList = new ArrayList<Integer>();
                        List<Number> numberList = intList;  // error
                        numberList.add(new Float(3.1415));  // error
                      


           F> 通配泛型(type wildcards) 雖可檢索元素(retrieve elements)與移除元素,但不能增加元素。

            [範(fàn)例]
          1
          2
          3
          4
          5
          6
            List<Integer> li = new ArrayList<Integer>( );
                        li.add(new Integer(42));
                        List<?> lu = li;  // OK
                        System.out.println(lu.get(0));
                        //lu.add(new Integer(43));  // error
                        lu.clear( );  // OK, don't depend on the compiler having to know anything about the type parameter of lu
                      


           G> 泛型在進行編譯時會進行 erasure 的過程,會刪除所有的泛型資訊。

            [範(fàn)例] 以下的範(fàn)例會產(chǎn)生錯誤:duplicate class
          1
          2
          3
          4
          5
          6
          7
            class ShoppingCart<T extends DVD>{
                          ……
                        }
                       
                        class ShoppingCart<T extends VideoTape>{
                          ……
                        }
                      


            [範(fàn)例] 以下的範(fàn)例會產(chǎn)生錯誤:name crash, have the same erasure
          1
          2
          3
          4
          5
          6
          7
          8
            class TwoForOneSpecial<T extends Rentable, W extends Rentable> {
                          public void add(T newRentable) {
                            ……
                          }
                          public void add(W newRentable) {
                            ……
                          }
                        }
                      


            [範(fàn)例]
          1
          2
          3
          4
          5
          6
          7
            public class GenericClass<E> {
                            ……
                            public Class<E> getElementType() {
                                return E.class;  // error
                            }
                            ……
                        }
                      


            [範(fàn)例]
          1
          2
          3
            Vector<String> x = new Vector<String>();
                        Vector<Integer> y = new Vector<Integer>();
                        return x.getClass() == y.getClass();  // true
                      


            [範(fàn)例]
          1
          2
          3
            LinkedList<Float> floatList = new LinkedList<Float>( );
                        List<Float> moreFloats = floatList;  // legal
                        LinkedList<Number> numberList = floatList;  // error
                      


            [範(fàn)例] parameterized type 可 cast 為 raw type
          1
          2
          3
          4
            List<Integer> ints = new LinkedList<Integer>( );
                        List oldList = ints;  // We can widen (due to backwards compatibility)
                        oldList.add("Hello");  // This line should be illegal, but it happily compiles and runs
                        Integer i = ints.get(0);  // compile warning, runtime error -- ClassCastException at runtime
                      


            [範(fàn)例] Breaking type safety with reflection
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
          35
          36
          37
            import java.util.ArrayList;
                        import java.util.List;
                       
                        public class BadIdea {
                          private static List<Integer> ints = new ArrayList<Integer>( );
                          public static void fillList(List<Integer> list) {
                            for (Integer i : list) {
                              ints.add( i );
                            } // for
                          } // fillList(List<Integer> list)
                       
                          public static void printList( ) {
                            for (Integer i : ints) {
                              System.out.println( i );
                            } // for
                          } // printList( )
                       
                          public static void main(String[] args) {
                            List<Integer> myInts = new ArrayList<Integer>( );
                            myInts.add(1);
                            myInts.add(2);
                            myInts.add(3);
                            System.out.println("Filling list and printing in normal way...");
                            fillList(myInts);
                            printList( );
                       
                            try {
                              List list = (List)BadIdea.class.getDeclaredField("ints").get(null);
                              list.add("Illegal Value!");
                            } catch (Exception e) {
                              e.printStackTrace( );
                            } // try-catch
                            System.out.println("Printing with illegal values in list...");
                            printList( );
                          } //main()
                        }
                      


           H> 陣列無法使用泛型,除非是一個通配符類型(unbounded wildcard)。
             可以宣告型態(tài)變數(shù)(concrete parameterized type)的陣列,但是不能使用 new 建立陣列。如 new T[100],因為型態(tài)變數(shù)會被 erasure。因此你只能指派此種陣列為 null。
          或是使用反射機制和類別實字(ClassName.class)解決。

            [範(fàn)例]
          1
          2
          3
          4
          5
          6
            List<String> [] lsa = new List<String>[10];  // 錯誤
                                = new List<?>[10];   // OK
                        Object o = isa;
                        Object[] oa = (Object[])o;
                        oa[1] = new ArrayList<Integer>( );
                        String s = lsa[1].get( );  // error
                      


          ***********************************************
          看完上面的文章之後,想測驗自己對 Generics 的了解,可以試試看下面的小測驗 Thumbs up
          http://www.grayman.de/quiz/java-generics-en.quiz

          Thumbs up 如果你有任何疑問,你幾乎可以在下列這個網(wǎng)站找到你要的解答 Thumbs up
          http://www.langer.camelot.de/GenericsFAQ/JavaGenericsFAQ.html#Fundamentals%20of%20Java%20Generics

          ***********************************************
          參考
          Generics FAQs
          了解泛型
          Generic Types, Part 1
          Erasure and Accidental Compile-Time Conflicts
          Not wild enough
          Wildcards in the Generics Specification
          http://www.devx.com/Java/Article/16024
          http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf --Sun 的教學(xué)
          http://www.davidflanagan.com/blog/000027.html --Generics Glossary

          無痛苦 generics
          http://www-106.ibm.com/developerworks/java/library/j-djc02113.html
          http://www-106.ibm.com/developerworks/java/library/j-djc03113.html
          http://www-106.ibm.com/developerworks/java/library/j-djc04093.html
          http://www-106.ibm.com/developerworks/java/library/j-djc05133.html

          posted on 2007-02-02 20:29 ???MengChuChen 閱讀(1137) 評論(0)  編輯  收藏 所屬分類: JavaBasic
          主站蜘蛛池模板: 新沂市| 安溪县| 安顺市| 固安县| 永登县| 张家港市| 宿州市| 宁南县| 靖安县| 汉沽区| 莆田市| 徐州市| 甘孜县| 北流市| 息烽县| 马龙县| 铁力市| 曲周县| 苏尼特右旗| 平乡县| 太仆寺旗| 镇原县| 施甸县| 嘉兴市| 大丰市| 唐山市| 正宁县| 高青县| 治多县| 武宣县| 郎溪县| 梁平县| 本溪市| 永安市| 旺苍县| 永德县| 万山特区| 塔城市| 启东市| 余江县| 登封市|