为什么在泛型类中不能使用原始类型和静态变量?
例如:
public class MyType<E> {
class Inner { }
static class Nested { }
public static void main(String[] args) {
MyType mt; // warning: MyType is a raw type
MyType.Inner inn; // warning: MyType.Inner is a raw type
MyType.Nested nest; // no warning: not parameterized type
MyType<Object> mt1; // no warning: type parameter given
MyType<?> mt2; // no warning: type parameter given (wildcard OK!)
}
}
MyType
是一个泛型类,而 Nested
是一个静态类。
调用 MyType.Inner
时不会警告泛型类型。
我想知道为什么 static
变量不会警告原始类型?
MyType<?>.Nested nest2;
,你会得到编译错误:“error: cannot select a static class from a parameterized type”。 - user207421