什么是多级通配符?语法混乱 [英] What are multi-level wild cards? Confusion in syntax
问题描述
我正在阅读来自 AngelikaLangerGenericsFaq 的多级通配符。我非常困惑
的语法。该文件说明
类型
集合< Pair< String,?>>> ;
是
泛型Collection接口的具体实例。它是
对不同类型的异构集合。它可以包含类型
Pair< String,Long>
,Pair< String,Date>
的元素,Pair< String,Object>
,
Pair< String,String>
等等等等。换句话说,
Collection
包含<$ c形式的不同类型>>
的组合$ c> Pair< String,?> 。
Collection <? extends< String,?>>> 是通配符
参数化类型;它不代表具体的参数化
类型。它代表来自collection集合
的一个代表,它们是Collection接口的实例,其中类型
参数的格式为Pair< String,?>
。兼容的实例
是集合< Pair< String,Long>>
,集合< Pair< String,String>> $
,
集合< Pair< String,Object>>
或集合< Pair< String,?>> ;
。在
中换句话说,我们不知道Collection的$
表示哪个实例。
作为一个经验法则,你必须阅读多
我对以下几点感到困惑。
- 有人可以用例子来详细说明这三个引号。我完全迷失在语法中
- 文档说,para-1是泛型类型的具体实例,而其他不是具体实例吗?这是怎么回事?
- 从头到尾阅读通配符意味着什么?
- 多级通配符有什么优势?
有人可以详细说明这些问题。谢谢。
有人可以用例子详细说明这三个引用。我完全迷失在语法中
那么,在这里再次写出这3个引号是没有意义的。因为,我不能给出比这更好的解释。相反,我会尝试回答下面的其他问题,那么你可能也会理解这个问题。如果没有,您可以再次询问您的查询。
文档说,para-1是一个泛型类型的具体实例化,其他的是不是具体的实例吗?这是什么?
一个具体实例是所有类型参数都是具体类型的参数,并且在编译时已知。例如, List< String>
是一个具体实例,因为 String
是一个具体类型。它的类型在编译时已知。而列表< ;? extends Number>
不是具体类型,因为?扩展Number
可以扩展 Number
的任何类型。所以,它的类型在编译时是未知的。类似地, Map< String,Integer>
是泛型类型 Map< K,V>
的具体实例。 p>
对于多级类型参数, 这就是为什么第一段说,它是 从头到尾阅读通配符意味着什么? 非专业人士的术语,它意味着从左到右。在确定参数化类型的类型时,首先看到最外面的类型参数。然后,如果该类型参数本身是一个参数化类型,那么您就转到该参数化类型的类型参数。因此,我们从左到右阅读了类型参数。 多级通配符的优点是什么? 假设您要创建水果列表。现在你的内部 I am reading multi-level wild cards from AngelikaLangerGenericsFaq. I am pretty confused
about the syntax. The document says List< List< extends
,外部的 List 是一个具体实例
,如果您的列表类型为 List< E>
,因为元素的类型在编译时已知为 List ,尽管内部的
与 List
是通配符因为存储元素的类型可以是 Integer
, Double
,的任何子类
。但该段只是谈论外部类型。而外部类型只能包含 List
类型。
Pair
的异构集合,因为实际的类型参数 Pair
可以是任何东西,但肯定是 Pair
,而不是其他东西。
List
可以包含任何种类的水果。一个苹果也是水果,而香蕉也是水果。所以,你必须确保你得到了所有这些。现在,由于泛型是不变的,因此列表<列表<水果>> $ c $,则无法添加
列表< Apple>
C>。为此,您需要使用通配符
,像这样 - List< List<现在可以使用
List< Apple>
, List< Banana> $ c $扩展Fruit>>
c>,任何水果的名单。
The type
Collection<Pair<String,?>>
is a concrete instantiation of the generic Collection interface. It is a heterogenous collection of pairs of different types. It can contain elements of typePair<String,Long>
,Pair<String,Date>
,Pair<String,Object>
,Pair<String,String>
, and so on and so forth. In other words,Collection<Pair<String,?>>
contains a mix of pairs of different types of the formPair<String,?>
.The type
Collection<? extends Pair<String,?>>
is a wildcard parameterized type; it does NOT stand for a concrete parameterized type. It stands for a representative from the family of collections that are instantiations of the Collection interface, where the type argument is of the formPair<String,?>
. Compatible instantiations areCollection<Pair<String,Long>>
,Collection<Pair<String,String>>
,Collection<Pair<String,Object>>
, orCollection<Pair<String,?>>
. In other words, we do not know which instantiation of Collection it stands for.As a rule of thumb, you have to read multi-level wildcards top-down.
I am confused about the following points.
- Can someone elaborate on these three quotes with example. I am totally lost into the syntax
- Document says, para-1 is the concrete instantiation of a generic type and other is not the concrete instantiation? How is that?
- What does it mean to read the wild-cards top down?
- What is the advantage of multi-level wild cards?
Can someone elaborate these points. Thanks.
Can someone elaborate on these three quotes with example. I am totally lost into the syntax
Well, it wouldn't make sense to write those 3 quotes again here. As, I can't give a better explanation than that. Instead, I will try to answer your other questions below, then possibly you will understand this one too. If not, you can ask your query again. I'll try to elaborate a little further then.
Document says, para-1 is the concrete instantiation of a generic type and other is not the concrete instantiation? How is that?
A concrete instantiation is the one in which all the type arguments are concrete type, and are known at compile time. For e.g., List<String>
is a concrete instantiation, because String
is a concrete type. It's type is known at compile time. Whereas, List<? extends Number>
is not a concrete type, because ? extends Number
can any type that extends Number
. So, it's type is unknown at compile time. Similarly, Map<String, Integer>
is a concrete instantiation of generic type Map<K, V>
.
In case of multi-level type parameters, List<List<? extends Number>>
, the outer List
is a concrete instantiation of List<E>
, because the type of elements is known to be a List
at compile time, although the inner List
is a wildcard instantiation, as the type of elements stored can be Integer
, Double
, any subclass of Number
. But that paragraph is talking about the outer type only. And the outer type can only contain List
type.
That's why the first paragraph said, it's a heterogenous collection of Pair
, because the actual type parameter of Pair
can be anything, but that is certain to be Pair
and nothing else.
What does it mean to read the wild-cards top down?
Talking in layman's term, it means from left-to-right. While determining the type of the parameterized type, you first see the outermost type parameter. Then if that type parameter is itself a parameterized type, then you move onto the type parameters of that parameterized type. So, we read the type parameters, from left-to-right.
What is the advantage of multi-level wild cards?
Suppose you want to create a List of List of Fruits. Now your inner List
can contain any kind of of fruits. An apple is also a fruit, and a banana is also a fruit. So, you have to make sure that you get all of them. Now, since generic types are invariant, in the sense, List<Apple>
is not the same as List<Fruit>
, you can't add a List<Apple>
if your type of list is List<List<Fruit>>
. For that you would need to use wildcards
like this - List<List<? extends Fruit>>
, which can now take List<Apple>
, List<Banana>
, list of any fruit.
这篇关于什么是多级通配符?语法混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!