一、泛型的内部原理(以集合为例)
泛型是提供给javac编译器使用的,可以限定集合中的输入类型,让编译器挡住源程序中的非法输入,编译器编译带类型说明的集合时会擦除掉“类型”信息,使程序运行效率不受影响。由于jvm是不知道类型信息的,所以只要能跳过编译器,就可以往某个泛型集合中加入其它类型的数据。参考代码:
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<String> list2 = new ArrayList<String>();
System.out.println(list1.getClass() == list2.getClass());//true,是同一个字节码对象
//用反射往类型参数为Integer的集合里添加String类型的元素
list1.getClass().getMethod("add", Object.class).invoke(list1, "helloworld");
System.out.println(list1.get(0)); //helloworld
二、泛型中的通配符
1、 任意类型: ?
2、上边界限定 : ? extends Object
3、下边界限定: ? super Integer
public class TypeGeneric {
public static void main(String[] args) throws Exception {
ArrayList<String> list2 = new ArrayList<String>();
printCollection(list2);
}
//Collection<Object>编译不通过,可改成Collection<?> / Collection<? extends Object>
//泛型中不考虑类型参数之间的继承关系
public static void printCollection(Collection<Object> collection){
//编译不通过,不确定传进来的类型是否有add方法
collection.add(“abc”)
//只能进行无类型的操作 如collection.size()
}
}
三、自定义泛型
1、自定义泛型方法:
public class TypeGeneric {
public static void main(String[] args) throws Exception {
Number add = add(3.5, 3);
Object abc = add(3, "abc");//返回类型是两个参数的共同类型(类型推断:取最大公约数)
}
public static <T>T add(T a,T b){
return b;
}
}
2、自定义泛型类
- 若在类中声明的泛型类型,则在方法上可以不用声明
- 如果方法为静态方法,则方法上依然要声明泛型类型,因为类上的泛型是在对象创建时指定具体类型的,而静态方法是不需要通过对象调用的,所以需要在方法上声明泛型
public class ArrayUtils<T> {
//交换数组中两个元素的位置
public void swap(T[] arr ,int pos1,int pos2){
T tmp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = tmp;
}
//倒序输出数组
public static <T> void reverse(T[] arr){
int start = 0;
int end = arr.length-1;
T tmp ;
while(start<=end){
tmp = arr[start];
arr[start] = arr[end];
arr[end] = tmp;
start++;
end --;
}
}
}
本文介绍了泛型的内部原理,特别是以集合为例展示了泛型如何限制非法输入。接着讨论了泛型中的通配符,包括任意类型、上边界限定和下边界限定。最后详细讲解了如何自定义泛型方法和泛型类,指出在类中声明泛型类型时,方法可以不声明,但静态方法需要在方法上明确泛型类型。
1591

被折叠的 条评论
为什么被折叠?



