5

当通配符“嵌套在另一个通配符中”时,我正在努力捕获通配符。是否可以?

编码:

public class ConvolutedGenerics {

    // listClass is a class implementing a List of some Serializable type
    public void doSomethingWithListOfSerializables(
            Class<? extends List<? extends Serializable>> listClass) {

        // Capture '? extends Serializable' as 'T extends Serializable'
        // The line does not compile with javac 7
        captureTheWildcard(listClass); // <------ zonk here
    }

    // listClass is a class implementing a List of some Serializable type
    private <T extends Serializable>
            void captureTheWildcard(
                    Class<? extends List</* ? extends */T>> listClass) {

        // Do something
    }
}

用 javac 7 编译产生:

ConvolutedGenerics.java:18: error: method captureTheWildcard in class ConvolutedGenerics cannot be applied to given types;
                captureTheWildcard(listClass);
                ^
  required: Class<? extends List<T>>
  found: Class<CAP#1>
  reason: no instance(s) of type variable(s) T exist so that argument type Class<CAP#1> conforms to formal parameter type Class<? extends List<T>>
  where T is a type-variable:
    T extends Serializable declared in method <T>captureTheWildcard(Class<? extends List<T>>)
  where CAP#1 is a fresh type-variable:
    CAP#1 extends List<? extends Serializable> from capture of ? extends List<? extends Serializable>
1 error

除了我发现的许多更简单的案例

但我无法从这些中推断出我的问题的答案。

4

2 回答 2

3

这是不可能的,你可能已经知道了。

让我用一个反例来说明:

List<Integer> foo = Arrays.asList(1,2,3);
List<String> bar = Arrays.asList("hi","mom");
List<List<? extends Serializable>> baz = Arrays.asList(foo, bar);
doSomethingWithListOfSerializables(baz);

public void doSomethingWithListOfSerializables(
        List<? extends List<? extends Serializable>> listList) {

    captureTheWildcard(listList); 
}

private <T extends Serializable>
        void captureTheWildcard(
                List<? extends List<T>> listList) {

    // Do something
}

应该T是什么?

于 2013-09-21T10:19:52.460 回答
0

您的代码的问题是您试图调用captureTheWildcard传递不同类型的参数,然后在此处定义:

private <T extends Serializable> void captureTheWildcard(Class<? extends List<T>> listClass)

您应该在方法定义中明确说明传递的参数实际上是 of 类型Class<? extends List<? extends Serializable>>或修改如下类型listClass

import java.util.List;
import java.io.Serializable;

public class ConvolutedGenerics {

    // listClass is a class implementing a List of some Serializable type                                                                           
    public <T extends Serializable> void doSomethingWithListOfSerializables(
                                                   Class<? extends List<T>> listClass) {

        // Capture '? extends Serializable' as 'T extends Serializable'                                                                             
        // The line does not compile with javac 7                                                                                                   
        captureTheWildcard(listClass); // <------ zonk here                                                                                         
    }

    // listClass is a class implementing a List of some Serializable type                                                                           
    private <T extends Serializable> void captureTheWildcard(Class<? extends List<T>> listClass) {

        // Do something                                                                                                                             
    }
}

使用 javac 1.7.0_25 编译良好

于 2013-09-20T08:45:23.103 回答