网站建设保教/抖音seo优化怎么做
通配符边界引入背景
使用泛型的过程中,经常出现一种很别扭的情况。我们有 Fruit
类,和它的派生类 Apple
类。
class Fruit {}
class Apple extends Fruit {}
然后有一个最简单的容器:Plate
类。盘子里可以放一个泛型的 “东西
”.
class Plate<T> {private T item;public Plate(T t) {item = t;}public void set(T t) {item = t;}public T get() {return item;}}
代码 | 我们想要表达的意思 | 实际效果 |
---|---|---|
Plate<Apple> p2 = new Plate<Apple> (new Apple()); | 定义一个"苹果盘子 " 装 苹果 | √ |
Plate<Fruit> p = new Plate<Apple> (new Apple()); | 定义一个"水果盘子 ",逻辑上水果盘子当然可以装苹果 。 | error: incompatible types: Plate<Apple> cannot be converted to Plate<Fruit> |
实际上,编译器
脑袋里认定的逻辑是这样的:
认可关系 | 不认可关系 |
---|---|
苹果 IS-A 水果 | 装苹果的盘子 NOT-IS-A 装水果的盘子 |
就算容器里装的东西之间有继承关系,但容器之间是没有继承关系的。所以我们不可以把Plate<Apple>
的引用传递给Plate<Fruit>
。
为了解决这种情况,Sun的大脑袋们就想出了<? extends T>
和<? super T>
的办法,来让”水果盘子
“和”苹果盘子
“之间发生关系。
<? extends T>
:上界通配符(Upper Bounds Wildcards)
☞包括T
在内的任何T
的子类
意义
Plate<Apple> | Plate<?extends Fruit> |
---|---|
一个能放苹果 的盘子 | 一个能放任意水果 的盘子 |
Plate<?extends Fruit> p = new Plate<Apple>(new Apple()); 可以用“ 苹果盘子 ”给“水果盘子 ”赋值了 | Plate<?extends Fruit> 是Plate<Fruit> 以及Plate<Apple> 的基类 |
练习
level1 | level2 | level3 | level4 |
---|---|---|---|
class Food{} | class Fruit extends Food{} | class Apple extends Fruit{} | class RedApple extends Apple{} |
class GreenApple extends Apple{} | |||
class Banana extends Fruit{} | |||
class Meat extends Food{} | class Pork extends Meat{} | ||
class Beef extends Meat{} |
Plate<? extends Fruit>
覆盖区域
level2 | level3 | level4 |
---|---|---|
class Fruit extends Food{} | class Apple extends Fruit{} | class RedApple extends Apple{} |
class GreenApple extends Apple{} | ||
class Banana extends Fruit{} |
<?superT>
:下界通配符(Lower Bounds Wildcards)
☞包括T
在内的任何T
的父类
意义
Plate<Fruit> | Plate<?super Fruit> |
---|---|
一个能放水果 以及一切水果基类 的盘子 | |
Plate<?super Fruit> 是Plate<Fruit> 的基类,但不是Plate<Apple> 的基类 |
练习
Plate<?super Fruit>
覆盖范围
level1 | level2 |
---|---|
class Food{} | class Fruit extends Food{} |
上下界通配符的副作用
边界让Java不同泛型之间的转换更容易了。这样的转换也有一定的副作用。那就是容器的部分功能失效
。
还是以刚才的 Plate
为例。我们可以对盘子做两件事,往盘子里set()
新东西,以及从盘子里get()
东西。
- 上界
<? extends T>
不能往里存,只能往外取
Plate<? extends Fruit> p=new Plate<Apple>(new Apple()); //不能存入任何元素
p.set(new Fruit()); //Error
p.set(new Apple()); //ErrorFruit newFruit1=p.get(); //读取出来的东西只能存放在Fruit或它的基类里
Object newFruit2=p.get();Apple newFruit3=p.get(); //Error
原因:
编译器只知道容器内是Fruit或者它的派生类
,但具体是什么类型不知道。可能是 Fruit
可能是 Apple
也可能是 Banana
,RedApple
,GreenApple
编译器在看到后面用 Plate
赋值以后,盘子里没有被标上有 “苹果
”。而是标上一个占位符:CAP#1
,来表示捕获一个 Fruit
或 Fruit的子类
,具体是什么类不知道,代号 CAP#1
。然后无论是想往里插入 Apple
或者 Meat
或者 Fruit
,译器都不知道能不能和这个 CAP#1
匹配,所以就都不允许。
- 下界
<? super Fruit>
不能往外取,只能往里存
Plate<? super Fruit> p=new Plate<Fruit>(new Fruit());//存入元素正常
p.set(new Fruit());
p.set(new Apple());Object newFruit2=p.get(); //读取出来的东西只能存放在Object类里Apple newFruit3=p.get(); //Error
Fruit newFruit1=p.get(); //Error
原因:
定义的元素是Fruit的基类
,那往里存 Fruit及其父类
都可以。但往外读取元素就费劲了,只有 所有类的基类
Object对象
才能装下。但这样的话,元素的类型信息就全部丢失。