数据结构系列18——泛型进阶(通配符以及通配符的上界与下界)

        之前总结过了泛型的基础知识,现在对泛型的通配符以及通配符的上界以及下界进行总结

链接:数据结构系列2——泛型_哈士奇的奥利奥的博客-CSDN博客

目录

1.通配符

2.通配符的上界

3.通配符的下界


1.通配符

package generic;

/**
 * Created with IntelliJ IDEA.
 * Description:泛型的进阶——通配符
 * User: YAO
 * Date: 2023-04-10
 * Time: 18:39
 */
class Message<T> {
    private T message ;
    public T getMessage() {
        return message;
    }
    public void setMessage(T message) {
        this.message = message;
    }
}
public class test1 {
    public static void main(String[] args) {
        Message<String> message1 = new Message<>();
        message1.setMessage("hello");
        //通过调用方法进行获取
        fun(message1);

        //如果创建了一个Integer的泛型对象
        Message<Integer> message2 = new Message<>();
        message2.setMessage(11);
        //这样就无法使用fun方法
//        fun(message2);
        //引入通配符?之后再进行代用函数,尽可以进行使用
        fun2(message1);
        fun2(message2);
    }
    public static void fun(Message<String> temp){
        System.out.println(temp.getMessage());
    }
    public static void fun2(Message<?> temp){
        System.out.println(temp.getMessage());
    }

}

2.通配符的上界

 通配符的上界,不能进行写入数据,只能进行读取数据。

public class test2 {
    /**
     * 1.测试泛型的上界
     */
    public static void main1(String[] args) {
        //1.实例化一个盘子对象
        Plate<Apple> applePlate = new Plate<>();
        //2.调用盘子类中的方法
        applePlate.setMessage(new Apple());
        Plate<Banana> bananaPlate = new Plate<>();
        bananaPlate.setMessage(new Banana());
        //3.进行获取盘子的信息
        func1(applePlate);
        func1(bananaPlate);

    }
    public static void func1(Plate<? extends Fruit> tmp){
        //tmp:传入的参数为Fruit的子类或者本身
        Apple apple = new Apple();
        //1.此时的tmp传入的不一定是apple,所以不能将传入,传入的只能是确定是tmp子类,但是这个子类不确定,因为tmp不确定。
        //tmp.setMessage(apple);
        //tmp.setMessage(new Apple());

        //2.但是可以进行接收,因为tmp的上界已经确定,只需要拿上界进行接收就可以,此时有可能发生向上转型
        Fruit fruit = tmp.getMessage();
        System.out.println(fruit);
    }
}

3.通配符的下界

 通配符的下界,不能进行读取数据,只能写入数据。

public class test2 {

    /**
     * 2.测试泛型的下界
     */
    public static void main(String[] args) {
        //1.实例化一个盘子对象
        Plate<Fruit> fruitPlate = new Plate<>();
        //2.调用盘子类中的方法
        fruitPlate.setMessage(new Apple());
        Plate<Food> foodPlate = new Plate<>();
        foodPlate.setMessage(new Banana());
        //3.通过泛型进行获取盘子的信息
        func2(fruitPlate);
        func2(foodPlate);
    }
    public static void func2(Plate<? super Fruit> tmp){
        // tmp:传入是Fruit的父类或者本身

        // 1.此时调用set方法是传入的参数可以确定为tmp的子类,因为tmp的下界已经确定,此时传入下界的子类即可
        Apple apple = new Apple();
        tmp.setMessage(apple);

        tmp.setMessage(new Apple());
        tmp.setMessage(new Banana());

        // 2.但是不可以接收,因为不确定是哪个父类或者是不是自己本身,只能直接输出。
        // Fruit fruit = tmp.getMessage();
        System.out.println(tmp.getMessage());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈士奇的奥利奥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值