泛型新理解

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;
    }

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值