java泛型类

来源:互联网 发布:淘宝企业店铺代理人 编辑:程序博客网 时间:2024/05/19 09:49

javaSE知识总结


java泛型

泛型:是指在定义类或者接口的时候可以为类和接口指定类型形参,在定义变量、定义方法是该类型形参可以当做普通的类型来使用,并且该类型形参在定义变量和创建对象的确定。

普通泛型:

只有一个泛型类型的:

class Point<T>{       // 此处可以随便写标识符号,T是type的简称      private T var ; // var的类型由T指定,即:由外部指定      public T getVar(){  // 返回值的类型由外部决定          return var ;      }      public void setVar(T var){  // 设置的类型也由外部决定          this.var = var ;      }  }public class GenericsDemo06{      public static void main(String args[]){          Point<String> p = new Point<String>() ; // 里面的var类型为String类型          p.setVar("it") ;        // 设置字符串          System.out.println(p.getVar().length()) ;   // 取得字符串的长度      }  }

带有两个泛型或更多的例子:

class Notepad<K,V>{       // 此处指定了两个泛型类型      private K key ;     // 此变量的类型由外部决定      private V value ;   // 此变量的类型由外部决定      public K getKey(){          return this.key ;      }      public V getValue(){          return this.value ;      }      public void setKey(K key){          this.key = key ;      }      public void setValue(V value){          this.value = value ;      }  }public class GenericsDemo09{      public static void main(String args[]){          Notepad<String,Integer> t = null ;     // 定义两个泛型类型的对象          t = new Notepad<String,Integer>() ;     // 里面的key为String,value为Integer          t.setKey("汤姆") ;     // 设置第一个内容          t.setValue(20) ;          // 设置第二个内容          System.out.print("姓名;" + t.getKey()) ;    // 取得信息          System.out.print(",年龄;" + t.getValue()) ;     // 取得信息      }  }

通配符

class Info<T>{      private T var ;     // 定义泛型变量      public void setVar(T var){          this.var = var ;      }      public T getVar(){          return this.var ;      }      public String toString(){   // 直接打印          return this.var.toString() ;      }  }  public class GenericsDemo14{      public static void main(String args[]){          Info<String> i = new Info<String>() ;       // 使用String为泛型类型          i.setVar("it") ;                            // 设置内容          fun(i) ;      }      public static void fun(Info<?> temp){     // 可以接收任意的泛型对象          System.out.println("内容:" + temp) ;      }  }   

受限制泛型

class Info<T> {    private T t;    public T getT() {        return t;    }    public void setT(T t) {        this.t = t;    }}public class GenericsDemo {    public static void main(String[] args) {        Info<String> in1=new Info<String>();        in1.setT("str");        Info<Object> in2=new Info<Object>();        in2.setT(new Object());        fun1(in1);        //fun1(in2);  //编译不通过,因为fun1的需要的参数是String或Object类型        fun2(in2);    }    public static void fun1(Info<? super String> temp){    // 只能接收String或Object类型的泛型          System.out.println(temp + "、") ;      }    public static void fun2(Info<? super Integer> temp){    // 只能接收Integer或Object类型的泛型          System.out.println(temp + "、") ;      }  }

泛型无法向上转型

Info<Object> in3=new Info<String>();//编译出错  Type mismatch: cannot convert from Info<String> to Info<Object>

泛型接口

interface Info<T>{        // 在接口上定义泛型      public T getVar() ; // 定义抽象方法,抽象方法的返回值就是泛型类型  }  class InfoImpl<T> implements Info<T>{   // 定义泛型接口的子类      private T var ;             // 定义属性      public InfoImpl(T var){     // 通过构造方法设置属性内容          this.setVar(var) ;        }      public void setVar(T var){          this.var = var ;      }      public T getVar(){          return this.var ;      }  }  public class GenericsDemo24{      public static void main(String arsg[]){          Info<String> i = null;        // 声明接口对象          i = new InfoImpl<String>("汤姆") ;  // 通过子类实例化对象          System.out.println("内容:" + i.getVar()) ;      }  }

泛型方法

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

泛型数组

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

泛型上界

上界用extends关键字声明,表示参数化的类型可能是所指定的类型,或者是此类型的子类

public void upperBound(List<? extends Date> list, Date date){      Date now = list.get(0);      System.out.println("now==>" + now);      //list.add(date); //这句话无法编译      list.add(null);//这句可以编译,因为null没有类型信息  }  

泛型下界

下界用super进行声明,表示参数化的类型可能是所指定的类型,或者是此类型的父类型,直至Object

public void lowerBound(List<? super Timestamp> list) {      Timestamp now = new Timestamp(System.currentTimeMillis());      list.add(now);      //Timestamp time = list.get(0); //不能编译  } 

泛型擦除

class Info<T> {    private T t;    public T getT() {        return t;    }    public void setT(T t) {        this.t = t;    }}public class GenericsDemo {    public static void main(String[] args) {        Info in4=new Info<String>();        Info in5=new Info<Integer>();        in4.setT(10);        in5.setT("10");        System.out.println(in4.getT().getClass());  //输出class java.lang.Integer        System.out.println(in5.getT().getClass());//输出class java.lang.String    }}

可以看到,当我们使用泛型擦除的时候,我们最后输出的类型实际上之和我们输入的数据类型相同了,而不是我们在使用new关键字中声明的类型了。

泛型擦除后,当前泛型的类型将自动变成为Object,所以当我们对其中的类型进行匹配的时候,只与我们输入的类型相比。

0 0