java泛型

来源:互联网 发布:windows反复重启 编辑:程序博客网 时间:2024/06/05 18:37

泛型类的应用

  • 泛型类使用方法如下:
public class ObjClass<T> {    private T x ;    private T y ;    public T getX() {        return x;    }    public void setX(T x) {        this.x = x;    }    public T getY() {        return y;    }    public void setY(T y) {        this.y = y;    }}
  • 多泛型的定义:
public class ObjClass<T,U> {    private T x ;    private U y ;    public T getX() {        return x;    }    public void setX(T x) {        this.x = x;    }    public U getY() {        return y;    }    public void setY(U y) {        this.y = y;    }}
  • 泛型的字母规范

    E — Element,常用在java Collection里,如: List,Iterator,Set
    K,V — Key,Value,代表Map的键值对
    N — Number,数字
    T — Type,类型,如String,Integer等等

  • 接口的泛型话

interface MsgClass<T> {    public T getMsg() ;    public void setMsg(T x);}
  • 泛型数组的使用:
  //定义    public static <T> T[] fun1(T...msg){  // 接收可变参数            return msg ;            // 返回泛型数组      }  //使用    public static void main(String args[]){        Integer i[] = fun1(8,9,8,44) ;        Integer[] result = fun1(i) ;  }

泛型的通配符

  • 泛型的传递有以下几个问题:
    1. 泛型的传递要保持一致性,就是已经定义好的泛型,不能再用其它的泛型所代替。
    2. 泛型如果想用其他的泛型所代替,就要使用<?>,但是使用了
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 demo1 {    public static void main(String args[]) {         // 使用String为泛型类型         Info<String> i = new Info<String>();                 i.setVar("ABCD");         //把String泛型类型的i对象传递给Object泛型类型的temp。         fun(i);                       }    // 接收Object泛型类型的Info对象    public static void fun(Info<Object> temp) {                System.out.println("内容:" + temp);    }}

会报出如下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:     The method fun(Info<Object>) in the type demo1 is not applicable for the arguments (Info<String>)    at Thread1.demo1.main(demo1.java:18)

此段报错表示,fun 方法接收的类型必须与传递的类型相一致,也就是i的类型必须是Info<T> 的不能再变成Info<Object> 的,如果非要接收的话可以用 Info 类型来接收,但是这样就失去了泛型的定义,这样使用失去了泛型的意义。

  • <?>接收泛型会产生的问题:
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 demo1{    public static void main(String args[]){        Info<?> i = new Info<String>() ;               i.setVar("ABCD") ;                                }};

此处代码会报错,问题是泛型为<?> 的类型又被指定为<String> 类型,报错如下:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:     The method setVar(capture#1-of ?) in the type Info<capture#1-of ?> is not applicable for the arguments (String)    at Thread1.demo1.main(demo1.java:17)

在使用”?“只能接收,不能修改。

泛型的上限

  • <T extends number> 定义T是number的子类型。
  • 利用<? extends Number>定义的变量,只可取其中的值,不可修改。

泛型的下限

  • <T super String> 表示填充为任意String类或者String的父类,此时就不能填写Integer 或者其他的类型。
  • 同样利用 <? super String> 定义的变量,只能取其中的值,不可以修改。

总结

  1. 使用?可以接受任意类型的泛型对象。
  2. 泛型的上限 :<? extentds 类型> 能取值,不能改变值。
  3. 泛型的下限 :<? super 类型> 只能取到类型或者类型的父类,不能取其他的类型,同样只能取值不能改变值。

java 泛型详解

0 0
原创粉丝点击