嵌套通配符下限 [英] Nested wildcards with lower bounds

查看:116
本文介绍了嵌套通配符下限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我通读了主要的Java Generic FAQ以及让我坚持的单一事物是嵌套的下界通配符。我想给你一个我明白的东西的例子,具体是哪些作品以及我如何看待它。也许你可以告诉我,即使编译器没有在好的情况下抱怨,我对这种想法的看法也是错误的。



示例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> 是一个子类型?
  • code>对< ;?扩展Number> 。是吗?
  • 是的,因为整数 Number 的子类型。

  • 示例2:


    1. List< Pair< Number>> List的子类型?延长Pair <?如果 Pair< Number> 是一个子类型
    2. code>对< ;?超级号码> 。是吗?
    3. 是的,因为 Number 是一个超类型 Number

    示例3:
    $ b


    1. List< Pair< Object>> List的子类<?超级配对<?如果 Pair< Object> 是一个超类型的超级数字>>
    2. code>对< ;?超级号码> 。是吗?
    3. 不,它不是。具有特定参数的参数化类型永远不能是具有通配符的参数化类型的超类型。

    示例4: / strong>


    $ b


    1. List< Pair< Integer>> 子类型列表与LT ;?超级配对<?如果 Pair< Integer> 是超类型的?
    2. code>对< ;?扩展Number> 。是吗?
    3. 不,它不是。具有特定参数的参数化类型永远不能是具有通配符的参数化类型的超类型。


    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:

    1. Is List<Pair<Integer>> a subtype of List<? extends Pair<? extends Number>>?
    2. It would be if Pair<Integer> is a subtype of Pair<? extends Number>. Is it?
    3. Yes, because Integer is a subtype of Number.

    Example 2:

    1. Is List<Pair<Number>> a subtype of List<? extends Pair<? super Number>>?
    2. It would be if Pair<Number> is a subtype of Pair<? super Number>. Is it?
    3. Yes, because Number is a supertype of Number.

    Example 3:

    1. Is List<Pair<Object>> a subtype of List<? super Pair<? super Number>>?
    2. It would be if Pair<Object> is a supertype of Pair<? super Number>. Is it?
    3. 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:

    1. Is List<Pair<Integer>> a subtype of List<? super Pair<? extends Number>>?
    2. It would be if Pair<Integer> is a supertype of Pair<? extends Number>. Is it?
    3. No, it is not. A parameterized type with a specific parameter can never be a supertype of a parameterized type with a wildcard.

    这篇关于嵌套通配符下限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆