使用泛型的好处是:在编译的时候检查类型的使用(转化)是否安全,并且所有转化都是自动和隐式的,以及提高了代码重用性。

[java]  view plain copy
  1. package org.example.fanxing;  
  2.   
  3. /** 
  4.  * DOC 类泛型事例一
     
  5.  * 泛型的类型参数只能是类类型(包括自定义类),不能是简单类型。 
  6.  *  
  7.  * @param  
  8.  */  
  9. public class SomeThing {  
  10.   
  11.     private T t;  
  12.   
  13.     public SomeThing(T t) {  
  14.         this.t = t;  
  15.     }  
  16.   
  17.     private void showType() {  
  18.         System.out.println(t.getClass().getName());  
  19.     }  
  20.   
  21.     private static class Test {  
  22.   
  23.         public static void main(String[] args) {  
  24.             new SomeThing("str").showType();  
  25.             new SomeThing(100).showType();  
  26.             new SomeThing('a').showType();  
  27.             // test results:  
  28.             /** 
  29.              * java.lang.String
     
  30.              * java.lang.Integer
     
  31.              * java.lang.Character 
  32.              */  
  33.         }  
  34.     }  
  35.   
  36. }  

[java]  view plain copy
  1. package org.example.fanxing;  
  2.   
  3. /** 
  4.  *  
  5.  * DOC 泛型示例2
     
  6.  * 假设要重构两个类,这两个类的变量和方法都一样,就是类型不一样,如StringSub和IntSub
     
  7.  * 现在重构这两个类,只要使用泛型就可以了 
  8.  *  
  9.  */  
  10. public class ObjectFanXing {  
  11.   
  12.     private T a;  
  13.   
  14.     public ObjectFanXing(T t) {  
  15.         this.a = t;  
  16.     }  
  17.   
  18.     public T getA() {  
  19.         return this.a;  
  20.     }  
  21.   
  22.     public void setA(T t) {  
  23.         this.a = t;  
  24.     }  
  25.   
  26.     private static class Test {  
  27.   
  28.         public static void main(String[] args) {  
  29.             System.out.println("string.getA=" + new ObjectFanXing("str").getA());  
  30.             System.out.println("double.getA=" + new ObjectFanXing(12.2222335).getA());  
  31.             System.out.println("object.getA=" + new ObjectFanXing(new Object()).getA());  
  32.             // test results:  
  33.             /** 
  34.              * string.getA=str
     
  35.              * double.getA=12.2222335
     
  36.              * object.getA=java.lang.Object@dc8569 
  37.              */  
  38.         }  
  39.     }  
  40.   
  41.     /** 
  42.      * private class StringSub { 
  43.      *  
  44.      * String a; 
  45.      *  
  46.      * public String getA() { return this.a; } 
  47.      *  
  48.      * public void setA(String a) { this.a = a; } 
  49.      *  
  50.      * } 
  51.      *  
  52.      * private class IntSub { 
  53.      *  
  54.      * private Integer a; 
  55.      *  
  56.      * public Integer getA() { return this.a; } 
  57.      *  
  58.      * public void setA(Integer a) { this.a = a; } } 
  59.      */  
  60.   
  61. }  

  62. [java]  view plain copy
    1. package org.example.fanxing;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.Collection;  
    5.   
    6. /** 
    7.  *  
    8.  * DOC 泛型示例3
       
    9.  * 带限制的泛型,可以限制传入的泛型为某个类的子类,或者实现了某个接口的类 
       
    10.  * ? 表示通用泛型
       
    11.  * 如果只指定了,而没有extends,则默认是允许Object及其下的任何Java类了。也就是任意类。
       
    12.  * 通配符泛型不单可以向下限制,如,还可以向上限制,如,表示类型只能接受Double及其上层父类类型,如Number、Object类型的实例。 
    13.  *  
    14.  *  
    15.  */  
    16. public class LimitFanxingextends Collection> {  
    17.   
    18.     private T t;  
    19.   
    20.     public LimitFanxing(T t) {  
    21.         this.t = t;  
    22.     }  
    23.   
    24.     public T getT() {  
    25.         return this.t;  
    26.     }  
    27.   
    28.     public void setT(T t) {  
    29.         this.t = t;  
    30.     }  
    31.   
    32.     public static class Test {  
    33.   
    34.         public static void main(String[] args) {  
    35.             LimitFanxing fanxing = new LimitFanxing(new ArrayList());  
    36.             LimitFanxingextends Collection> fanxing2 = new LimitFanxing(new ArrayList());  
    37.         }  
    38.     }  
    39.   
    40. }  

    你可能感兴趣的:(java,泛型使用,JAVA)