Java泛型--<? extends>和<? super>

Java泛型--目录


概述

  • <? extends T> 表示类型的上界,表示参数化类型的可能是T 或是 T的子类

  • <? super T> 表示类型下界(Java Core中叫超类型限定),表示参数化类型是此类型的超类型(父类型),直至Object

具体讲解请参加如下示例:

public class SuperExtneds {
    public static void main(String[] args) {
        List<? extends Apple> list1 = getExtendsAppleList();
        Apple first = list1.get(0);
        list1.add(null);

        first.sayHello();

        //编译错误
        //list.add(new Fruit());
        //list.add(new Apple())
        //list.add(new Object());

        List<? super Apple> list2 = getSupperAppleList();
        list2.add(new Apple());
        list2.add(new HFSApple());
        //只能返回obj
        Object item = list2.get(0);

        //编译 错误
        //Fruit aa1 = list2.get(0);
        //Apple aa2 = list2.get(0);
        //HFSApple aa3 = list2.get(0);
        //Orange aa4 = list2.get(0);
        //list2.add(new Fruit());

        List<Fruit> list3 = new ArrayList<>();
        handlerApple(list3);
        list3.get(0).sayHello();

    }

    public static List<? extends Apple> getExtendsAppleList() {
        List<Apple> appleList = new ArrayList<>();
        appleList.add(new Apple());
        appleList.add(new HFSApple());

        //编译错误
        //return new ArrayList<Fruit>();

        return appleList;
    }

    public static List<? super Apple> getSupperAppleList() {
        List<Fruit> fruitList = new ArrayList<>();
        fruitList.add(new Fruit());
        fruitList.add(new Orange());

        //编译错误
        //return new ArrayList<HFSApple>();

        return fruitList;
    }

    public static void handlerApple(List<? super Apple> list) {
        list.add(new HFSApple());
        list.add(new Apple());

        //编译报错
        //list.add(new Fruit());
        //list.add(new Orange());
    }
}

class Fruit {
    public void sayHello() {
        System.out.println("hello");
    }
}

class Apple extends Fruit {
}

class HFSApple extends Apple {
}

class Orange extends Fruit {
}

    List<? extends Apple> list1 只能获取某个类型的对象,不能向其中添加Fruit,Orange甚至Apple的实例,这是为什么呢?原来是? expends Apple的含义时Apple或者Apple的子类,这是一个不确定的类型,那么List就不知道到底应该装入什么类型的对象,所以不允许添加任何类型的对象,但是奇怪的事我们可用向其中添加null,因为null表示任何对象.

    List<? super Apple> list2能够向其中添加Apple,HFSApple等Apple及其子类,但是不能添加Apple的父类,在获取list2中的对象时只能是Object,这是为什么呢?原来? super Apple表示Apple的或者Apple的父类,有一个有限长度的类型范围,直到Object,既然有范围,就可以在这个范围之上向list2添加Apple或者Apple的子类,但是在获取的时候,因为时一个有限的范围,list2不知道应该返回具体持有的是什么类型,所以只能返回所有类型的父类Objct.

    list3 简单表述了? super Apple的一个应用场景,

    那么我们做出如下总结:

  • extends 可用于的返回类型限定,不能用于参数类型限定。

  • super 可用于参数类型限定,不能用于返回类型限定。

eg:

    讲到这里,想必大家都明白了其中的奥秘,在这里给大家出个问题,解释一下在下面两个例子中注释掉的return语句为什么编译报错:

public static List<? extends Apple> getExtendsAppleList() {
    List<Apple> appleList = new ArrayList<>();
    appleList.add(new Apple());
    appleList.add(new HFSApple());

    //编译错误
    //return new ArrayList<Fruit>();

    return appleList;
}
public static List<? super Apple> getSupperAppleList() {
    List<Fruit> fruitList = new ArrayList<>();
    fruitList.add(new Fruit());
    fruitList.add(new Orange());

    //编译错误
    //return new ArrayList<HFSApple>();

    return fruitList;
}


转载于:https://my.oschina.net/wjzk/blog/602245

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值