简体   繁体   English

Java泛型:了解有界通配符

[英]Java Generics : Understanding Bounded Wildcards

In Generics chapter, one sentence got my attention. 在“ 泛型”一章中,一句话引起了我的注意。 For the following declaration List<? extends Shape> 对于以下声明List<? extends Shape> List<? extends Shape> , it says: List<? extends Shape> ,它说:

Note: It could be Shape itself, or some subclass; 注意:它可以是Shape本身,也可以是某些子类。 it need not literally extend Shape . 它不需要从字面上扩展Shape

What does not literally in this context mean ? 在此上下文中的字面意思是什么?

Shape could be an interface and <? extends Shape> Shape可以是接口, <? extends Shape> <? extends Shape> could be an instance of a class that implements the Shape interface. <? extends Shape>可以是实现Shape接口的类的实例。

It means that the class does not have to extend Shape , ie be a direct subclass of Shape . 这意味着该类不必 extend Shape ,即作为直接子Shape It could also be Shape itself, or it could be an indirect subclass of Shape , or Shape could be an interface which the class implements . 它也可以Shape自身,也可以是间接子Shape ,或Shape可以是一个interface ,其类implements What the author is trying to convey is that the keyword extends is used out of convenience (adding a new keyword to a language is expensive) and familiarity, it does not mean that every type that can be used has to have exactly the string extends Shape in its definition. 什么是作者试图传达的是关键字extends用于进出方便(添加新的关键字的语言是昂贵的)和熟悉,这并不意味着每一个可以使用的类型都有一个完全相同的字符串extends Shape在其定义中。

In other words: the author literally means "literally" with "literally". 换句话说:作者的字面意思是“字面”和“字面”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM