泛型类的通配符

Box

package com.jiang.obj4;

public class Box<T> {
    private T first;

    public T getFirst() {
        return first;
    }

    public void setFirst(T first) {
        this.first = first;
    }
}

测试

package com.jiang.obj4;

public class Test {
    public static void main(String[] args) {
        Box<Number> box1=new Box<>();
        box1.setFirst(100);
        showBox(box1);
    }
    public static void showBox(Box<Number> box){
        Number first = box.getFirst();
        System.out.println(first);
    }
}

1.假如现在我想弄个Box<Integer> box2=new Box<>();,并使用showBox方法。

package com.jiang.obj4;

public class Test {
    public static void main(String[] args) {
        Box<Number> box1=new Box<>();
        box1.setFirst(100);
        showBox(box1);

        Box<Integer> box2=new Box<>();
        box2.setFirst(200);
        showBox(box2); 
        此处报错---
        虽然这里的IntegerNumber子类,但是泛型不可以考虑多态
    }

    此处不能够用多态去理解
    public static void showBox(Box<Number> box){
        Number first = box.getFirst();
        System.out.println(first);
    }

2.怎么办?假如加一个public static void showBox(Box<Integer> box)
但是:报错
实质上还是同一类型:都是BOX ,故会报错
(同一泛型类的不同对象类型都是一样的)

package com.jiang.obj4;

public class Test {
    public static void main(String[] args) {
        Box<Number> box1=new Box<>();
        box1.setFirst(100);
        showBox(box1);

        Box<Integer> box2=new Box<>();
        box2.setFirst(200);
        showBox(box2);
    }

    此处不能够用多态去理解
    public static void showBox(Box<Number> box){
        Number first = box.getFirst();
        System.out.println(first);
    }

    实质上还是同一类型:都是BOX 故会报错
    public static void showBox(Box<Integer> box){
        Number first = box.getFirst();
        System.out.println(first);
    }

3.使用通配符来替代两种:

  • java public static void showBox(Box<Number> box){
  • java public static void showBox(Box<Integer> box){
  • public static void showBox(Box<?> box){
package com.jiang.obj4;

public class Test {
    public static void main(String[] args) {
        Box<Number> box1=new Box<>();
        box1.setFirst(100);
        showBox(box1);

        Box<Integer> box2=new Box<>();
        box2.setFirst(200);
        showBox(box2);

        //Box<Integer> box2=new Box<>();
        //box2.setFirst(200);
        //showBox(box2);

    }
    不想用object
    public static void showBox(Box<?> box){
        Object first = box.getFirst(); //object接受相关
        System.out.println(first);
    }

3.但是不想用object,转换怎么办?
类型通配符的上限:

  • public static void showBox(Box<? extends Number> box){
    要么是Number要么是子类,都赋值给Number(类似于向上转型 )

下限:

  • public static void showBox(Box<? super Number> box){
    Number及其父类
package com.jiang.obj4;

public class Test {
    public static void main(String[] args) {
        Box<Number> box1=new Box<>();
        box1.setFirst(100);
        showBox(box1);

        Box<Integer> box2=new Box<>();
        box2.setFirst(200);
        showBox(box2);
    }
   上限   最大到number(子类)
    public static void showBox(Box<? extends Number> box){
        Number first = box.getFirst(); //object接受相关
        System.out.println(first);
    }

上限

Animal—Cat—MiniCat

public class Animal {
}
public class Cat extends Animal{
}
public class MiniCat extends Cat{
}

测试

  • public static void showAnimal(ArrayList<? extends Cat> list){
  • Cat及其子类可以使用
package com.jiang.obj4;

import java.util.ArrayList;

public class Test_extends {
    public static void main(String[] args) {
        ArrayList<Animal> animals=new ArrayList<>();
        ArrayList<Cat> cats=new ArrayList<>();
        ArrayList<MiniCat> miniCats=new ArrayList<>();

        showAnimal(animals);----此处报红
        showAnimal(cats);
        showAnimal(miniCats);

    }
    public static void showAnimal(ArrayList<? extends Cat> list){
        for (Cat cat : list) {
            System.out.println(cat);
        }
    }
}

1.上限通配符不能对list进行添加
类型不确定:
假如传进去的是Minicatlist,但添加的是list.add(new Cat()),不安全、

 public static void showAnimal(ArrayList<? extends Cat> list){
        list.add(new Cat());---报红
        list.add(new MiniCat());---报红
        for (Cat cat : list) {
            System.out.println(cat);
        }
    }

下限

  • public static void showAnimal(ArrayList<? super Cat> list){
  • Cat及其父类可以使用
  • 可以对list进行add操作。(但是不保证约束要求)
package com.jiang.obj4;

import java.util.ArrayList;

public class Test_extends {
    public static void main(String[] args) {
        ArrayList<Animal> animals=new ArrayList<>();
        ArrayList<Cat> cats=new ArrayList<>();
        ArrayList<MiniCat> miniCats=new ArrayList<>();

        showAnimal(animals);
        showAnimal(cats);
        showAnimal(miniCats);---报红

    }

    //下限
    public static void showAnimal(ArrayList<? super Cat> list){
        for (Object o : list) {
            System.out.println(o);
        }
    }
}
   public static void showAnimal(ArrayList<? super Cat> list){
        list.add(new MiniCat());
        list.add(new Cat());
        for (Object o : list) {
        //Cat与其父类用object接受
            System.out.println(o);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值