java泛型学习,泛型通配符的理解

来源:互联网 发布:专业照片恢复软件 编辑:程序博客网 时间:2024/05/17 01:31

通配符:

<? extends A>

向上造型一个泛型对象的引用

简单点说:就是只能使用父类和这个父类的子类,比如说<? extends Number>, 那么像Float, Integer都能使用.

//********? extends Fruit********
  //*********引用指向继承 Fruit的子类*******
  List<Apple> aps=new ArrayList<Apple>();
  aps.add(new Apple("qqq"));
  List<Fruit> fs=new ArrayList<Fruit>();
  List<? extends Fruit> list=null;
  //可以把子类集合赋值给它
  list=aps;
  //报错
  list.add(new Fruit());
  //报错
  list.add(new Apple());

//取数据,能定位到具体的实例
  Fruit a= list.get(0);

从继承叫角度讲,父类的引用是指向子类的实例的,但是你在把子类型存储到父类集合时,确保安全性不允许你加入子类型,也不知道子类型到底是什么,是那个.

所以<? extends A> 是不允许插入的,但是我能在实际情况下是可以获取数据,经常用于数据查询的时候.

 

<? super A>

向下造型一个泛型对象的引用

简单点说:就是只能用他本身和他的父类,比如说<? super Integer>,那么像Float就不能用,只能使用Integer,Number.

//**********? super Apple***********
  //********引用指向Apple的超级类(父类)********
  List<? super Apple> as=null;
  List<RedApple> rs=new ArrayList<RedApple>();
  List<Apple> aas=new ArrayList<Apple>();
  List<Fruit> ffs=new ArrayList<Fruit>();
  //错误
  as=rs;
  //可以赋值自己
  as=aas;
  //可以把父类集合赋值给它
  as=ffs;
  //把Apple赋值给它
  as.add(new Apple());
  //吧Apple的子类赋值给它
  as.add(new RedApple());
  //出错
  as.add(new Fruit());

//取数据的话,就是Object,不知道具体到那个超级类
  as.get(0);

从继承的角度讲,<? super Apple>引用是指向父类的, 所以不管是什么类型的超级类, 他的子类或者子类的子类都继承与它, 所以可以存放子类的实例, 但是存放自己不行,

不知道自己具体是那个超级类,所以取出来的是Object.

 

总结 ? extends 和 the ? super 通配符的特征,我们可以得出以下结论:

◆ 如果你想从一个数据类型里获取数据,使用 ? extends 通配符

◆ 如果你想把对象写入一个数据结构里,使用 ? super 通配符

◆ 如果你既想存,又想取,那就别用通配符。

 

 

以下是网上转的泛型应用例子:

 

下面例子有个T...arg 参数,表示可变参数.如果一个方法有多个参数包括可变参数,可变参数要放在最后哦,上次就搞错了.

 

普通泛型

Java代码 
  1. class Point<T>{       // 此处可以随便写标识符号,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<String> p = new Point<String>() ; // 里面的var类型为String类型  
  13.         p.setVar("it") ;        // 设置字符串  
  14.         System.out.println(p.getVar().length()) ;   // 取得字符串的长度  
  15.     }  
  16. };  
  17. ----------------------------------------------------------  
  18. class Notepad<K,V>{       // 此处指定了两个泛型类型  
  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<String,Integer> t = null ;        // 定义两个泛型类型的对象  
  37.         t = new Notepad<String,Integer>() ;       // 里面的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. };  

 通配符

Java代码 
  1. class Info<T>{  
  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<String> i = new Info<String>() ;       // 使用String为泛型类型  
  16.         i.setVar("it") ;                            // 设置内容  
  17.         fun(i) ;  
  18.     }  
  19.     public static void fun(Info<?> temp){     // 可以接收任意的泛型对象  
  20.         System.out.println("内容:" + temp) ;  
  21.     }  
  22. };  

 受限泛型

Java代码 
  1. class Info<T>{  
  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<Integer> i1 = new Info<Integer>() ;        // 声明Integer的泛型对象  
  16.         Info<Float> i2 = new Info<Float>() ;            // 声明Float的泛型对象  
  17.         i1.setVar(30) ;                                 // 设置整数,自动装箱  
  18.         i2.setVar(30.1f) ;                              // 设置小数,自动装箱  
  19.         fun(i1) ;  
  20.         fun(i2) ;  
  21.     }  
  22.     public static void fun(Info<? extends Number> temp){  // 只能接收Number及其Number的子类  
  23.         System.out.print(temp + "、") ;  
  24.     }  
  25. };  
  26. ----------------------------------------------------------  
  27. class Info<T>{  
  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<String> i1 = new Info<String>() ;      // 声明String的泛型对象  
  42.         Info<Object> i2 = new Info<Object>() ;      // 声明Object的泛型对象  
  43.         i1.setVar("hello") ;  
  44.         i2.setVar(new Object()) ;  
  45.         fun(i1) ;  
  46.         fun(i2) ;  
  47.     }  
  48.     public static void fun(Info<? super String> temp){    // 只能接收String或Object类型的泛型  
  49.         System.out.print(temp + "、") ;  
  50.     }  
  51. };  

 泛型无法向上转型

Java代码 
  1. class Info<T>{  
  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<String> i1 = new Info<String>() ;      // 泛型类型为String  
  16.         Info<Object> i2 = null ;  
  17.         i2 = i1 ;                               //这句会出错 incompatible types  
  18.     }  
  19. };  

 泛型接口

Java代码 
  1. interface Info<T>{        // 在接口上定义泛型  
  2.     public T getVar() ; // 定义抽象方法,抽象方法的返回值就是泛型类型  
  3. }  
  4. class InfoImpl<T> implements Info<T>{   // 定义泛型接口的子类  
  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<String> i = null;        // 声明接口对象  
  19.         i = new InfoImpl<String>("汤姆") ;  // 通过子类实例化对象  
  20.         System.out.println("内容:" + i.getVar()) ;  
  21.     }  
  22. };  
  23. ----------------------------------------------------------  
  24. interface Info<T>{        // 在接口上定义泛型  
  25.     public T getVar() ; // 定义抽象方法,抽象方法的返回值就是泛型类型  
  26. }  
  27. class InfoImpl implements Info<String>{   // 定义泛型接口的子类  
  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. };  

 泛型方法

Java代码 
  1. class Demo{  
  2.     public <T> 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. };  

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

Java代码 
  1. class Info<T extends 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<Integer> i = fun(30) ;  
  16.         System.out.println(i.getVar()) ;  
  17.     }  
  18.     public static <T extends Number> Info<T> fun(T param){//方法中传入或返回的泛型类型由调用方法时所设置的参数类型决定  
  19.         Info<T> temp = new Info<T>() ;      // 根据传入的数据类型实例化Info  
  20.         temp.setVar(param) ;        // 将传递的内容设置到Info对象的var属性之中  
  21.         return temp ;   // 返回实例化对象  
  22.     }  
  23. };  

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

Java代码 
  1. class Info<T>{    // 指定上限,只能是数字类型  
  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<String> i1 = new Info<String>() ;  
  16.         Info<String> i2 = new Info<String>() ;  
  17.         i1.setVar("HELLO") ;        // 设置内容  
  18.         i2.setVar("汤姆") ;       // 设置内容  
  19.         add(i1,i2) ;  
  20.     }  
  21.     public static <T> void add(Info<T> i1,Info<T> i2){  
  22.         System.out.println(i1.getVar() + " " + i2.getVar()) ;  
  23.     }  
  24. };  

 泛型数组

Java代码 
  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> T[] fun1(T...arg){  // 接收可变参数  
  7.         return arg ;            // 返回泛型数组  
  8.     }  
  9.     public static <T> void fun2(T param[]){   // 输出  
  10.         System.out.print("接收泛型数组:") ;  
  11.         for(T t:param){  
  12.             System.out.print(t + "、") ;  
  13.         }  
  14.     }  
  15. };  

 泛型的嵌套设置

Java代码 
  1. class Info<T,V>{      // 接收两个泛型类型  
  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<S>{  
  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<Info<String,Integer>> d = null ;       // 将Info作为Demo的泛型类型  
  36.         Info<String,Integer> i = null ;   // Info指定两个泛型类型  
  37.         i = new Info<String,Integer>("汤姆",30) ;    // 实例化Info对象  
  38.         d = new Demo<Info<String,Integer>>(i) ; // 在Demo类中设置Info类的对象  
  39.         System.out.println("内容一:" + d.getInfo().getVar()) ;  
  40.         System.out.println("内容二:" + d.getInfo().getValue()) ;  
  41.     }  
  42. };  

 

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

 public static <E> ArrayList<E> newArrayList() {
    return new ArrayList<E>();
  }

 

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

    。。。
             return Lists.newArrayList();
    }

 

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

 

原创粉丝点击