1. 泛型的作用
防止随意的放置任何的对象, 使用泛型后只能按我们使用时指定的类型添加以及会相应的进行编译时检查, 在编译检查后会去除相应的泛型信息 (运行时没有这个信息了), 在类型转换的也会自动的相应的转换为相应的信息
- public class GenenricyDemo1 {
- public static <T> T test1(T t){
- return t;
- }
- public <T> T test2(T t){
- return t;
- }
- public static void main(String[] args) {
- String test1 = GenenricyDemo1.test1("a");
- String test2 = new GenenricyDemo1().test2("b");
- }
- }
2. 泛型的编译时类型检查 (泛型是没有多态. 直接比较放入类的 class 文件, 但是在数组中是可以有多态的)
- public class GenericyDemo2 {
- public static void main(String[] args) {
- // ArrayList<Object> arrayList = new ArrayList<String>();// 编译时错误
- ArrayList<String> arrayList = new ArrayList<>();// 编译时正常
- ArrayList arrayList1 = new ArrayList<String>();// 编译时正常
- ArrayList<Object> arrayList2=arrayList1 ;// 编译时正常, 编译时一行一行检查的
- // arrayList2=arrayList;// 编译时错误
- //Vector<String> [] vct=new Vector<String>[10]; // 编译错误, 参数化类型没有数组的表达形式
- //ArrayList<int> str=null;// 编译错误, 泛型参数只能对对象不能是基本类型的数据, 不会自动包装
- Object [] objs= new String [10];// 编译时通过
- interface GenericyDemo9Iface<GenericyDemo>{//? extends GenericyDemo9Fater 是不能使用的 (类 接口等)
- }
- }
3. 继承实现泛型
- @SuppressWarnings("all")
- public class GenericyDemo3<B, A, C> extends GenericyDemo3Father<A> implements GenericyDemo3Iface<B> {
- private static final String ParameterizedType = null;
- private C c;
- @Override
- public String test(B t) {
- return t.toString();
- }
- private <T> T test3(T t) {
- return t;
- }
- public static void main(String[] args) {
- Type[] genericInterfaces = GenericyDemo3.class.getGenericInterfaces();
- System.out.println(genericInterfaces[0]);
- Type superclass = GenericyDemo3.class.getGenericSuperclass();
- if (superclass instanceof ParameterizedType) {
- Type[] actualTypeArguments = ((ParameterizedType) superclass).getActualTypeArguments();
- System.out.println(actualTypeArguments[0]);
- }
- GenericyDemo3 genericyDemo3 = new GenericyDemo3();
- try {
- Method[] methods = genericyDemo3.getClass().getDeclaredMethods();
- for (Method method : methods) {
- if (method.getName().equals("test3")) {
- method.setAccessible(true);
- Object invoke = method.invoke(genericyDemo3, "String");
- if (invoke.getClass() == String.class) {
- System.out.println(invoke);// 有输出 String
- // 因为我们获取的字节码文件还是自己本身
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- interface GenericyDemo3Iface<T> {
- public String test(T t);
- }
- class GenericyDemo3Father<T> {
- public String test1(T t) {
- return t.toString();
- }
- }
4. 泛型不写与书写 Obejct 的区别
- public class GenericyDemo4 {
- // 不写泛型默认为 Object, 编译时不会进行类型检查
- // 写泛型为 Object, 编译时不会进行类型检查
- public static void main(String[] args) {
- ArrayList arrayList = new ArrayList();
- arrayList.add("a");
- arrayList.add(1);
- ArrayList<Object> arrayList1= new ArrayList<>();
- arrayList1.add("a");
- arrayList1.add(1);
- }
- }
5. 获取泛型的类型
- public class GenericyDemo5 {
- public static void main(String[] args) throws Exception {
- Method[] methods = GenericyDemo5Test.class.getMethods();
- for (Method method : methods) {
- if (method.getName().equals("test")) {
- Type[] parameterTypes = method.getGenericParameterTypes();
- System.out.println(parameterTypes[0]);
- }
- }
- }
- }
- class GenericyDemo5Test {
- public Vector<String> test(Vector<String> Str) {
- return null;
- }
- }
6. 泛型的拭除
- public class GenericyDemo6 {
- public static void main(String[] args) {
- ArrayList<String> arrayList = new ArrayList<>();
- ArrayList<Integer> arrayList1 = new ArrayList<>();
- System.out.println(arrayList.getClass()==arrayList1.getClass());//true
- }
- }
7. 通配符的特征:
1) 可以用于声明类型和声明方法的参数上, 实例化时需指定, 不可以用于声明类的参数上;
2)? 只能输入和输出, 不能修改;
3)? extends 上限 (继承或者实现关系);
4)? super 下限 (继承或者实现关系, 可以是下限或者是下限的子类);
- public class GenericyDemo7 {
- public static void main(String[] args) {
- ArrayList<? super List<String>> arrayList1 = new ArrayList<>();
- arrayList1.add(new ArrayList<>());// 不能添加 new Object(); 原因前面已经说明
- }
- }
- public class GenericyDemo7 {
- public static void main(String[] args) {
- ArrayList<? extends GenericyDemo7Iface> list1 = new ArrayList<>();
- ArrayList<GenericyDemo7Son> list2 = new ArrayList<>();
- list2.add(new GenericyDemo7Son());
- list1=list2;
- System.out.println(list1.get(0));
- //list1.add(new GenericyDemo7Son());// 编译错误, 只能添加 null 值
- }
- }
- class GenericyDemo7Son implements GenericyDemo7Iface {
- }
- interface GenericyDemo7Iface {
- }
来源: http://www.bubuko.com/infodetail-2687021.html