Chinaunix首页 | 论坛 | 博客
  • 博客访问: 269854
  • 博文数量: 88
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 840
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-20 21:13
文章分类

全部博文(88)

文章存档

2022年(1)

2017年(1)

2016年(2)

2015年(1)

2014年(83)

分类: Java

2014-07-31 17:13:53

普通泛型

  1. class Point{       // 此处可以随便写标识符号,T是type的简称  
  2.     private T var ; // var的类型由T指定,即:由外部指定  
  3.     public T getVar(){  // 返回值的类型由外部决定  
  4.         return var ;  
  5.     }  
  6.     public void setVar(T var){  // 设置的类型也由外部决定  
  7.         this.var = var ;  
  8.     }  
  9. };  
  10. public class GenericsDemo06{  
  11.     public static void main(String args[]){  
  12.         Point p = new Point() ; // 里面的var类型为String类型  
  13.         p.setVar("it") ;        // 设置字符串  
  14.         System.out.println(p.getVar().length()) ;   // 取得字符串的长度  
  15.     }  
  16. };  
  17. ----------------------------------------------------------  
  18. class Notepad{       // 此处指定了两个泛型类型  
  19.     private K key ;     // 此变量的类型由外部决定  
  20.     private V value ;   // 此变量的类型由外部决定  
  21.     public K getKey(){  
  22.         return this.key ;  
  23.     }  
  24.     public V getValue(){  
  25.         return this.value ;  
  26.     }  
  27.     public void setKey(K key){  
  28.         this.key = key ;  
  29.     }  
  30.     public void setValue(V value){  
  31.         this.value = value ;  
  32.     }  
  33. };  
  34. public class GenericsDemo09{  
  35.     public static void main(String args[]){  
  36.         Notepad t = null ;        // 定义两个泛型类型的对象  
  37.         t = new Notepad() ;       // 里面的key为String,value为Integer  
  38.         t.setKey("汤姆") ;        // 设置第一个内容  
  39.         t.setValue(20) ;            // 设置第二个内容  
  40.         System.out.print("姓名;" + t.getKey()) ;      // 取得信息  
  41.         System.out.print(",年龄;" + t.getValue()) ;       // 取得信息  
  42.   
  43.     }  
  44. };  

 通配符

  1. class Info{  
  2.     private T var ;     // 定义泛型变量  
  3.     public void setVar(T var){  
  4.         this.var = var ;  
  5.     }  
  6.     public T getVar(){  
  7.         return this.var ;  
  8.     }  
  9.     public String toString(){   // 直接打印  
  10.         return this.var.toString() ;  
  11.     }  
  12. };  
  13. public class GenericsDemo14{  
  14.     public static void main(String args[]){  
  15.         Info i = new Info() ;       // 使用String为泛型类型  
  16.         i.setVar("it") ;                            // 设置内容  
  17.         fun(i) ;  
  18.     }  
  19.     public static void fun(Info temp){     // 可以接收任意的泛型对象  
  20.         System.out.println("内容:" + temp) ;  
  21.     }  
  22. };  

 受限泛型

  1. class Info{  
  2.     private T var ;     // 定义泛型变量  
  3.     public void setVar(T var){  
  4.         this.var = var ;  
  5.     }  
  6.     public T getVar(){  
  7.         return this.var ;  
  8.     }  
  9.     public String toString(){   // 直接打印  
  10.         return this.var.toString() ;  
  11.     }  
  12. };  
  13. public class GenericsDemo17{  
  14.     public static void main(String args[]){  
  15.         Info i1 = new Info() ;        // 声明Integer的泛型对象  
  16.         Info i2 = new Info() ;            // 声明Float的泛型对象  
  17.         i1.setVar(30) ;                                 // 设置整数,自动装箱  
  18.         i2.setVar(30.1f) ;                              // 设置小数,自动装箱  
  19.         fun(i1) ;  
  20.         fun(i2) ;  
  21.     }  
  22.     public static void fun(Infoextends Number> temp){  // 只能接收Number及其Number的子类  
  23.         System.out.print(temp + "、") ;  
  24.     }  
  25. };  
  26. ----------------------------------------------------------  
  27. class Info{  
  28.     private T var ;     // 定义泛型变量  
  29.     public void setVar(T var){  
  30.         this.var = var ;  
  31.     }  
  32.     public T getVar(){  
  33.         return this.var ;  
  34.     }  
  35.     public String toString(){   // 直接打印  
  36.         return this.var.toString() ;  
  37.     }  
  38. };  
  39. public class GenericsDemo21{  
  40.     public static void main(String args[]){  
  41.         Info i1 = new Info() ;      // 声明String的泛型对象  
  42.         Info i2 = new Info() ;      // 声明Object的泛型对象  
  43.         i1.setVar("hello") ;  
  44.         i2.setVar(new Object()) ;  
  45.         fun(i1) ;  
  46.         fun(i2) ;  
  47.     }  
  48.     public static void fun(Infosuper String> temp){    // 只能接收String或Object类型的泛型  
  49.         System.out.print(temp + "、") ;  
  50.     }  
  51. };  
  52.  泛型无法向上转型

    1. class Info{  
    2.     private T var ;     // 定义泛型变量  
    3.     public void setVar(T var){  
    4.         this.var = var ;  
    5.     }  
    6.     public T getVar(){  
    7.         return this.var ;  
    8.     }  
    9.     public String toString(){   // 直接打印  
    10.         return this.var.toString() ;  
    11.     }  
    12. };  
    13. public class GenericsDemo23{  
    14.     public static void main(String args[]){  
    15.         Info i1 = new Info() ;      // 泛型类型为String  
    16.         Info i2 = null ;  
    17.         i2 = i1 ;                               //这句会出错 incompatible types  
    18.     }  
    19. };  
    20.  泛型接口

      1. interface Info{        // 在接口上定义泛型  
      2.     public T getVar() ; // 定义抽象方法,抽象方法的返回值就是泛型类型  
      3. }  
      4. class InfoImpl implements Info{   // 定义泛型接口的子类  
      5.     private T var ;             // 定义属性  
      6.     public InfoImpl(T var){     // 通过构造方法设置属性内容  
      7.         this.setVar(var) ;    
      8.     }  
      9.     public void setVar(T var){  
      10.         this.var = var ;  
      11.     }  
      12.     public T getVar(){  
      13.         return this.var ;  
      14.     }  
      15. };  
      16. public class GenericsDemo24{  
      17.     public static void main(String arsg[]){  
      18.         Info i = null;        // 声明接口对象  
      19.         i = new InfoImpl("汤姆") ;  // 通过子类实例化对象  
      20.         System.out.println("内容:" + i.getVar()) ;  
      21.     }  
      22. };  
      23. ----------------------------------------------------------  
      24. interface Info{        // 在接口上定义泛型  
      25.     public T getVar() ; // 定义抽象方法,抽象方法的返回值就是泛型类型  
      26. }  
      27. class InfoImpl implements Info{   // 定义泛型接口的子类  
      28.     private String var ;                // 定义属性  
      29.     public InfoImpl(String var){        // 通过构造方法设置属性内容  
      30.         this.setVar(var) ;    
      31.     }  
      32.     public void setVar(String var){  
      33.         this.var = var ;  
      34.     }  
      35.     public String getVar(){  
      36.         return this.var ;  
      37.     }  
      38. };  
      39. public class GenericsDemo25{  
      40.     public static void main(String arsg[]){  
      41.         Info i = null;      // 声明接口对象  
      42.         i = new InfoImpl("汤姆") ;    // 通过子类实例化对象  
      43.         System.out.println("内容:" + i.getVar()) ;  
      44.     }  
      45. };  

       泛型方法

      1. class Demo{  
      2.     public  T fun(T t){            // 可以接收任意类型的数据  
      3.         return t ;                  // 直接把参数返回  
      4.     }  
      5. };  
      6. public class GenericsDemo26{  
      7.     public static void main(String args[]){  
      8.         Demo d = new Demo() ;   // 实例化Demo对象  
      9.         String str = d.fun("汤姆") ; //   传递字符串  
      10.         int i = d.fun(30) ;     // 传递数字,自动装箱  
      11.         System.out.println(str) ;   // 输出内容  
      12.         System.out.println(i) ;     // 输出内容  
      13.     }  
      14. };  

       通过泛型方法返回泛型类型实例

      1. class Infoextends Number>{ // 指定上限,只能是数字类型  
      2.     private T var ;     // 此类型由外部决定  
      3.     public T getVar(){  
      4.         return this.var ;     
      5.     }  
      6.     public void setVar(T var){  
      7.         this.var = var ;  
      8.     }  
      9.     public String toString(){       // 覆写Object类中的toString()方法  
      10.         return this.var.toString() ;      
      11.     }  
      12. };  
      13. public class GenericsDemo27{  
      14.     public static void main(String args[]){  
      15.         Info i = fun(30) ;  
      16.         System.out.println(i.getVar()) ;  
      17.     }  
      18.     public static extends Number> Info fun(T param){//方法中传入或返回的泛型类型由调用方法时所设置的参数类型决定  
      19.         Info temp = new Info() ;      // 根据传入的数据类型实例化Info  
      20.         temp.setVar(param) ;        // 将传递的内容设置到Info对象的var属性之中  
      21.         return temp ;   // 返回实例化对象  
      22.     }  
      23. };  

       使用泛型统一传入的参数类型

      1. class Info{    // 指定上限,只能是数字类型  
      2.     private T var ;     // 此类型由外部决定  
      3.     public T getVar(){  
      4.         return this.var ;     
      5.     }  
      6.     public void setVar(T var){  
      7.         this.var = var ;  
      8.     }  
      9.     public String toString(){       // 覆写Object类中的toString()方法  
      10.         return this.var.toString() ;      
      11.     }  
      12. };  
      13. public class GenericsDemo28{  
      14.     public static void main(String args[]){  
      15.         Info i1 = new Info() ;  
      16.         Info i2 = new Info() ;  
      17.         i1.setVar("HELLO") ;        // 设置内容  
      18.         i2.setVar("汤姆") ;       // 设置内容  
      19.         add(i1,i2) ;  
      20.     }  
      21.     public static  void add(Info i1,Info i2){  
      22.         System.out.println(i1.getVar() + " " + i2.getVar()) ;  
      23.     }  
      24. };  

       泛型数组

      1. public class GenericsDemo30{  
      2.     public static void main(String args[]){  
      3.         Integer i[] = fun1(1,2,3,4,5,6) ;   // 返回泛型数组  
      4.         fun2(i) ;  
      5.     }  
      6.     public static  T[] fun1(T...arg){  // 接收可变参数  
      7.         return arg ;            // 返回泛型数组  
      8.     }  
      9.     public static  void fun2(T param[]){   // 输出  
      10.         System.out.print("接收泛型数组:") ;  
      11.         for(T t:param){  
      12.             System.out.print(t + "、") ;  
      13.         }  
      14.     }  
      15. };  

       泛型的嵌套设置

      1. class Info{      // 接收两个泛型类型  
      2.     private T var ;  
      3.     private V value ;  
      4.     public Info(T var,V value){  
      5.         this.setVar(var) ;  
      6.         this.setValue(value) ;  
      7.     }  
      8.     public void setVar(T var){  
      9.         this.var = var ;  
      10.     }  
      11.     public void setValue(V value){  
      12.         this.value = value ;  
      13.     }  
      14.     public T getVar(){  
      15.         return this.var ;  
      16.     }  
      17.     public V getValue(){  
      18.         return this.value ;  
      19.     }  
      20. };  
      21. class Demo{  
      22.     private S info ;  
      23.     public Demo(S info){  
      24.         this.setInfo(info) ;  
      25.     }  
      26.     public void setInfo(S info){  
      27.         this.info = info ;  
      28.     }  
      29.     public S getInfo(){  
      30.         return this.info ;  
      31.     }  
      32. };  
      33. public class GenericsDemo31{  
      34.     public static void main(String args[]){  
      35.         Demo> d = null ;       // 将Info作为Demo的泛型类型  
      36.         Info i = null ;   // Info指定两个泛型类型  
      37.         i = new Info("汤姆",30) ;    // 实例化Info对象  
      38.         d = new Demo>(i) ; // 在Demo类中设置Info类的对象  
      39.         System.out.println("内容一:" + d.getInfo().getVar()) ;  
      40.         System.out.println("内容二:" + d.getInfo().getValue()) ;  
      41.     }  
      42. };  

       

       泛型方法不一定要通过参数来确定泛型准确类型,可以只通过返回值,比如:

       public static ArrayList newArrayList() {
          return new ArrayList();
        }

       

          public List queryHistories(Long skyid,PrepaidHistoryType type, Date from, Date end) {

          。。。
                   return Lists.newArrayList();
          }

       

      这样Lists.newArrayList();
      智能的知道返回类型为PrepaidHistory

      阅读(518) | 评论(0) | 转发(0) |
      给主人留下些什么吧!~~