您的位置:首页 > 房产 > 建筑 > 泛型新理解

泛型新理解

2024/10/6 20:25:32 来源:https://blog.csdn.net/qq_53374893/article/details/140557817  浏览:    关键词:泛型新理解

1.创建三个类,并写好对应关系

package com.jmj.gulimall.study;public class People {
}
package com.jmj.gulimall.study;public class Student extends People{
}
package com.jmj.gulimall.study;public class Teacher extends People{
}

2.解释一下这三个方法

  public static <T,U extends T> void test(T a,U b){}public static <T extends People,U extends T> void test(T a,U b){}public static <T ,U extends T> void test(Class<T> tClass,T a,U b){}

方法一 : 因为泛型会有类型擦除,所以在编译器被擦除为 Object 所以编译器检测不出 你的 a 和 b是否有继承关系,因为都是Object 类型

方法二 :因为显示 约束了 T 的类型, 泛型会被擦除为People 类型的下届或者同级,所以编译器能够约束好 U的类型 是T 的同类或子类

方法三 : 因为给了 T的字节码对象,所以字节码对象是不会在编译期 擦除,所以编译还是知道 T是什么类型,也能够检测出 U的类型是不是 T的同类或子类

3.类型擦除

其实就是 把原泛型的方法

比如这个方法, T 和U类型全部变成 Object

相当于

  public static  void test(Object a,Object b){}

 若有返回值,他会在编译期间加上强制类型转换成你传入进去的类型,相同的类型,保证类型安全转换,若是不同类型,则会在编译期间报错,告诉你类型不正确,这就是 类型擦除!!!

4.? 类型 相当于 Object 然后它可以 加 extend 或者 super

super 表示?的下届  extend 表示?的上界, 一般用在集合泛型比较多,

加了这个是可以进行集合的强转泛型的,不加是转不了的

1、第一种用法

 public static void main(String[] args) {List<? extends People> students = new ArrayList<>();List<? super Student> objects = new ArrayList<>();Student people = (Student) students.get(0);objects= (List<? super Student>) students;students= (List<? extends People>) objects;}

2、第二种用法 

  public static <T extends People> List<T> getList(){List<Student> objects = new ArrayList<>();return (List<T>) objects;}public static void main(String[] args) {List<Student> list = getList();}

3、第三种用法 

  public static  List<? super Student> getList(){List<People> objects = new ArrayList<>();return  objects;}public static void main(String[] args) {List<People> list = (List<People>) getList();System.out.println(list);}

4、第四种用法 

  public static void main(String[] args) {ArrayList<Student> students = new ArrayList<>();students.add(new Student());List<? extends People> list =students;List<People> peopleList = (List<People>) list;People people = peopleList.get(0);System.out.println(people);ArrayList<People> peopleArrayList = new ArrayList<>();peopleArrayList.add(new People());List<? super Student> arrayList = peopleArrayList;List<Student> a = (List<Student>) arrayList;System.out.println(a);}

5、第五种用法 

    public static <T extends People> T max(List<? extends T> list) {List<? extends T> list1 = list;T t = list.get(0);return null;}public static void main(String[] args) {max(new ArrayList<Student>());}

6、第六种用法

    public static <T extends People> T max(List<? super T> list,Class<T> tClass) {return null;}public static void main(String[] args) {max(new ArrayList<Student>(), Son.class);}

7、第七种用法 

    public static <T extends People> T max(List<? super T> list,Class<T> tClass) {List<? super T> list1 = list;List< T> a = (List<T>) list1;return null;}public static void main(String[] args) {max(new ArrayList<Gard>(), People.class);}

 

5.安卓里面 View继承,拿到子类对象,是这么拿的

public static <T extends People> T getList(){return (T) new Student();}public static void main(String[] args) {Student list = getList();}

但是转的不对会抛 强制类型转换异常

 public <T extends View> T findViewById(int id) {// 查找当前视图的子视图中是否存在指定 id 的视图if (id == NO_ID) {return null;}// 在当前视图中查找视图View result = findViewById(id, this);return (T) result;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com