Java 泛型:了解有界通配符
Java Generics : Understanding Bounded Wildcards
在Generics章中,有一句话引起了我的注意。对于以下声明 List<? extends Shape>
,它表示:
Note: It could be Shape itself, or some subclass; it need not literally extend Shape.
在此上下文中,不是字面意思是什么意思?
Shape
可以是接口,<? extends Shape>
可以是实现 Shape
接口的 class 的实例。
这意味着 class 没有 到 extend Shape
,即 Shape
的直接子 class .它也可以是 Shape
本身,或者它可以是 Shape
的间接子 class,或者 Shape
可以是 interface
而 class implements
。作者试图传达的是关键字 extends
的使用是出于方便(向语言添加新关键字是昂贵的)和熟悉,它确实 而不是 意味着可以使用的每种类型都必须在其定义中 恰好 字符串 extends Shape
。
换句话说:作者字面上的意思是"literally"和"literally"。
在Generics章中,有一句话引起了我的注意。对于以下声明 List<? extends Shape>
,它表示:
Note: It could be Shape itself, or some subclass; it need not literally extend Shape.
在此上下文中,不是字面意思是什么意思?
Shape
可以是接口,<? extends Shape>
可以是实现 Shape
接口的 class 的实例。
这意味着 class 没有 到 extend Shape
,即 Shape
的直接子 class .它也可以是 Shape
本身,或者它可以是 Shape
的间接子 class,或者 Shape
可以是 interface
而 class implements
。作者试图传达的是关键字 extends
的使用是出于方便(向语言添加新关键字是昂贵的)和熟悉,它确实 而不是 意味着可以使用的每种类型都必须在其定义中 恰好 字符串 extends Shape
。
换句话说:作者字面上的意思是"literally"和"literally"。