嵌套通配符下限 [英] Nested wildcards with lower bounds
问题描述
示例1(有意义):
static void WildcardsMethod(List< ;? extends Pair< ;? extends Number>> list)
{
System.out.println(It worked);
}
static void TestWildcardsMethod()
{
List< Pair< Integer>> list = null;
WildcardsMethod(list);
}
我首先查看WildcardMethod签名中最深的通配符。它正在寻找 Pair <?扩展Number>
。因此,我可以使用 Pair< Integer>
, Pair< Double>
等等。现在,如果我决定用 Pair< Integer>
代替 Pair <?>,我会在脑海中看到下面的代码。 extends Number>
:
List< ;?延长Pair<整数>>
现在,通配符表示参数化类型的类型/子类型 Pair<整数>
。因此,我可以将 Pair< Integer>
或 SubPair< Integer>
传递给WildcardsMethod。
示例2(合理):
static void WildcardsMethod(List <?extends Pair<?super Number>> list)
{
System.out.println(It worked);
}
static void TestWildcardsMethod()
{
List< Pair< Number>> list = null;
WildcardsMethod(list);
}
我看,我首先需要一个 Pair< ;? super number>
所以我决定传入 Pair< Number>
,导致下面的代码:
?延长Pair< Number>
然后查看最左边的通配符,看看我可以使用 Pair< ; Number>
或 SubPair< Number>
。我最终传递了 List< Pair< Number>>
。
通配符作为最内层边界(数字)的子类型或超类型。然后我去到顶级通配符并寻找泛型类型的子类型/超类型(Pair)。
示例3(没有意义) :
static void WildcardsMethod(List< ;? super pair< ;? super number>> list)
{
System.out.println(It worked);
}
static void TestWildcardsMethod()
{
List< Pair< Object>> list = null;
WildcardsMethod(list);
}
那么,就 Pair <? super Number>
,Object绝对是Number的超类型,所以 Pair< Object>
应该像前面的例子一样工作。以下是我想了解这一点时的想法:
?超级对< Object>
所以我只限于 Pair< Object>
或 SuperPair< Object>
。然而,这些都不起作用。
示例4(没有意义):
static void WildcardsMethod(List< ;? super pair< ;? extends Number>> list)
{
System.out.println(It works );
}
static void TestWildcardsMethod()
{
List< Pair< Integer>> list = null;
WildcardsMethod(list);
}
这里是一样的。 Pair< Integer>
属于对的族<?扩展数字>
导致以下结果:
?超级对<整数>
然后我可以传入 Pair< Integer>
或 SuperPair< Integer>
但是,这也不起作用。
因此,我要么考虑到这个错误,不知道该模型适用于扩展,但不适用于超级,或者我只是缺少有关低位和嵌套通配符的信息。
示例1: List< Pair< Integer>>
List的子类型<?延长Pair <?如果
Pair< Integer>
是一个子类型?
整数
是 Number
的子类型。
示例2:
- 是
List< Pair< Number>>
List的子类型?延长Pair <?如果
Pair< Number>
是一个子类型 code>对< ;?超级号码> 。是吗? - 是的,因为
Number
是一个超类型Number
。
示例3:
$ b
- 是
List< Pair< Object>>
List的子类<?超级配对<?如果
?Pair< Object>
是一个超类型的超级数字>> code>对< ;?超级号码> 。是吗? - 不,它不是。具有特定参数的参数化类型永远不能是具有通配符的参数化类型的超类型。
示例4: / strong>
$ b
-
List< Pair< Integer>>
子类型列表与LT ;?超级配对<?如果
Pair< Integer>
是超类型的? code>对< ;?扩展Number> 。是吗? - 不,它不是。具有特定参数的参数化类型永远不能是具有通配符的参数化类型的超类型。
So I read through the main Java Generic FAQ and the single thing which is holding me up are nested wildcards with lower bounds. I want to give you an example of what I do understand, something specifically which works and how I view it. Maybe you could tell me the way I am thinking about this is wrong even though the compiler isn't complaining in the "good" case.
Example 1 (makes sense):
static void WildcardsMethod(List<? extends Pair<? extends Number>> list)
{
System.out.println("It worked");
}
static void TestWildcardsMethod()
{
List<Pair<Integer>> list = null;
WildcardsMethod(list);
}
I first look at the deepest wildcard and bound in WildcardMethod's signature. It is looking for Pair<? extends Number>
. Therefore, I could use Pair<Integer>
, Pair<Double>
and so on. Now I have something which looks like the below code in my mind if I decided to substitute Pair<Integer>
for Pair<? extends Number>
:
List<? extends Pair<Integer>>
Now, the wildcard represents a type/subtype of the parametrized type Pair<Integer>
. Therefore, I can either pass a Pair<Integer>
or SubPair<Integer>
to WildcardsMethod.
Example 2 (makes sense):
static void WildcardsMethod(List<? extends Pair<? super Number>> list)
{
System.out.println("It worked");
}
static void TestWildcardsMethod()
{
List<Pair<Number>> list = null;
WildcardsMethod(list);
}
I look and see that I first need a Pair<? super Number>
so I decide to pass in Pair<Number>
resulting in the following code:
? extends Pair<Number>
I then look at the leftmost wildcard and see that I can use either Pair<Number>
or SubPair<Number>
. I end up passing List<Pair<Number>>
.
So in other words, I see the deepest wildcard as asking for a subtype or supertype of the innermost bound (Number). I then go to the top level wildcard and look for a subtype/supertype of the generic type (Pair).
Example 3 (doesn't make sense):
static void WildcardsMethod(List<? super Pair<? super Number>> list)
{
System.out.println("It worked");
}
static void TestWildcardsMethod()
{
List<Pair<Object>> list = null;
WildcardsMethod(list);
}
Well, in terms of Pair<? super Number>
, Object is definitely a supertype of Number so Pair<Object>
should work just as it did for the previous examples. The following is what I think of when trying to understand this:
? super Pair<Object>
So I am limited to either Pair<Object>
or SuperPair<Object>
. However, none of this works.
Example 4 (doesn't make sense):
static void WildcardsMethod(List<? super Pair<? extends Number>> list)
{
System.out.println("It worked");
}
static void TestWildcardsMethod()
{
List<Pair<Integer>> list = null;
WildcardsMethod(list);
}
It's the same thing here. Pair<Integer>
belongs to the family of Pair<? extends Number>
resulting in the following:
? super Pair<Integer>
I can then pass in either Pair<Integer>
or SuperPair<Integer>
However, this too does not work.
So I am either thinking of this wrong and somehow that model works for extends but not for super or I am simply missing something about lowerbounds and nested wildcards.
Example 1:
- Is
List<Pair<Integer>>
a subtype ofList<? extends Pair<? extends Number>>
? - It would be if
Pair<Integer>
is a subtype ofPair<? extends Number>
. Is it? - Yes, because
Integer
is a subtype ofNumber
.
Example 2:
- Is
List<Pair<Number>>
a subtype ofList<? extends Pair<? super Number>>
? - It would be if
Pair<Number>
is a subtype ofPair<? super Number>
. Is it? - Yes, because
Number
is a supertype ofNumber
.
Example 3:
- Is
List<Pair<Object>>
a subtype ofList<? super Pair<? super Number>>
? - It would be if
Pair<Object>
is a supertype ofPair<? super Number>
. Is it? - No, it is not. A parameterized type with a specific parameter can never be a supertype of a parameterized type with a wildcard.
Example 4:
- Is
List<Pair<Integer>>
a subtype ofList<? super Pair<? extends Number>>
? - It would be if
Pair<Integer>
is a supertype ofPair<? extends Number>
. Is it? - No, it is not. A parameterized type with a specific parameter can never be a supertype of a parameterized type with a wildcard.
这篇关于嵌套通配符下限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!