泛型擦除

来源:互联网 发布:java中级工程师学校 编辑:程序博客网 时间:2024/06/05 13:35

泛型 只在编译期段有效。
我们可以在运行期把 泛型擦除。

package test.proxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
* Created by 1dian_tech50 on 2016/9/18.
* User : linving
* Date : 2016/9/18
* Time : 15:25
*/
public class TestT {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
List<\Integer> integerList = new ArrayList<>();
integerList.add(1);
Class c = integerList.getClass();
Method method = c.getMethod(“add”,Object.class);
method.invoke(integerList,”文字”);
System.out.print(integerList);
}

}

一个 List<\Integer> List 可以 放入 字符串
说明了 泛型被擦除了。

0 0
原创粉丝点击