泛型Generic

https://www.cnblogs.com/ssh-html/p/10780398.html

1. jdk1.5新增特性

泛型是提供给javac编译器使用的,可以限定集合中的输入类型,
让编译器挡住源程序中的非法输入,
编译器编译带类型说明的集合时会去掉“类型”信息,
使得程序运行不受影响,对于参数化的泛型类型,
getClass() 方法的返回值和原始类型完全一样。
由于编译生成的字节码会去掉泛型的类型信息,只要能跳过编译器,
就可以往某个泛型集合中加入其它类型的数据,
例如,用反射得到集合,在调用其add()方法即可。

	ArrayList<String> collection1 = new ArrayList<String>();
    collection1.add("123");

    ArrayList<Integer> collection2 = new ArrayList<Integer>();
    collection2.add(11);

    // 结果为 true 实际编译完成之后 实际上是相同的基本类型
    System.out.println(collection1.getClass() == collection2.getClass());
    // 利用反射可以跳过编译器
    collection2.getClass().getMethod("add", Object.class).invoke(collection2, "abc");
    System.out.println(collection2.get(1));

ArrayList<E> 这种是泛型类型;
E称为泛型变量或者是泛型参数;
ArrayList<Integer>称为参数化的类型;
Integer称为类型参数的实例或者是实际类型参数;
ArrayList为原始类型

2. 泛型中的 ? 通配符

使用?通配符可以引用各种参数化的类型,?通配符定义的变量只是用作引用,可以调用与参数化无关的方法,不能调用与参数化有关的方法

通配符的扩展:

1) 限定通配符的上边界
正确:Vector<? extends Number> x = new Vector<Integer>();
这种是后面new出来的可以是extends 的子类,例如:Integer 是Number 的子类,是继承关系
错误:Vector<? extends Number> x = new Vector<String>();

2)限定通配符的下边界
正确:Vector<? super Integer> x = new Vector<Number>();
这种是后面new出来的是前面的父类,正好和上面的相反,例如:Number 是Integer的父类
错误:Vector<? super Integer> x = new Vector<Byte>();

3. 泛型集合类

// 简单的map中元素的迭代
  Map<String,Integer> maps = new HashMap<String,Integer>();
        maps.put("A", 27);
        maps.put("B", 28);
        
        Set<Map.Entry<String, Integer>> entrySet = maps.entrySet();
        for(Map.Entry<String, Integer> entry : entrySet){
            System.out.println(entry.getKey() + ":" + entry.getValue());
      }

4. 自定义泛型方法和应用

1、只有引用类型才能作为泛型方法的实际参数,swap(new int[3]{1,2,3},1,2)语句会报编译错误
2、除了在应用泛型时可以使用extends限定符,在定义泛型的时候也可以使用extends限定符
3、普通方法、构造方法和静态方法中都可以使用泛型
4、在泛型中可以同时有多个类型参数,在定义他们的尖括号中用逗号分,例如:
public static <K,V> v getValue(K key){return map.getKey()};

1     // 交换数组中任意下标中两个元素的位置
2     private static <T> void swap(T[] a, int i, int j){
3         T tmp = a[i];
4         a[i] = a[j];
5         a[j] = tmp;
6     }

5、当一个变量被声明为泛型时,只能被实例变量和方法调用(还有内嵌类型),而不能被静态变量和方法调用,因为静态成员是被所有的参数化的类所共享的,所以静态成员不应该有类级别的类型参数

// 例如baseDao中经常那样定义成泛型类,注意:静态成员没有类级别的类型参数
public class GenericDao <T>{
    public void add(T t){
    }
    public T findById(int id){
        return null;
    }
    public void delete(T obj){ 
    }
    public void delete(int id){   
    }
    public T update(T obj){
        return null;
    }
    public Set<T> findByConditions(String where){
            return null;
    }
}

5. 通过反射获取泛型的实际类型参数

框架的灵魂就是反射

// 为什么用Vector.class而不用Vector<Date>.class 后面的本身就是错误的,在编译之后会将泛型擦除掉,其实本质还是Vector
    Method applyMethod = GenericTest.class.getMethod("applyVector", Vector.class);
    Type[] types = applyMethod.getGenericParameterTypes();
    ParameterizedType pType = (ParameterizedType) types[0];
    System.out.println(pType.getRawType());
    System.out.println(pType.getActualTypeArguments()[0]);

    // 将需要获取泛型类型的变量交给一个方法,当做参数来获取其中参数的实际类型
    public static void applyVector(Vector<Date> v1){   
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值