在创建具有泛型类型的类时,似乎不可能使用私有类作为类型参数,即使该类是泛型类型的内部类。考虑这段代码:
import java.util.Iterator;
import test.Test.Type;
public class Test implements Iterable<Type> {
@Override
public Iterator<Type> iterator() {return null;}
static class Type {}
}
上面的示例可以编译,而同一示例在私有时无法编译:Type
import java.util.Iterator;
// ERROR: The type test.Test.Type is not visible
import test.Test.Type;
// ERROR: Type cannot be resolved to a type
public class Test implements Iterable<Type> {
@Override
// ERROR: The return type is incompatible with Iterable<Type>.iterator()
public Iterator<Type> iterator() {return null;}
private static class Type {}
}
为什么不能将私有类用作其封闭类的类型参数?尽管是私有的,但在我看来,该类Type
应该在类中可见Test
。