我想知道这个创作是否可行:
Class<String> stringClass = String.class;
MyGenericClass<stringClass> myGenericClass;
Run Code Online (Sandbox Code Playgroud)
我在我的IDE中尝试过,但是出现了编译错误.
Java语言规范说明了类型参数
类型参数可以是引用类型或通配符.通配符在需要仅部分了解类型参数的情况下非常有用.
stringClass在此示例中,对象不是引用类型,也不是通配符.
类型参数,<>在参数化类型变量声明,构造函数或方法调用表达式等之间放置的部分,只能是引用类型或通配符.例如,
MyGenericClass<String> variable;
// or
MyGenericClass<?> variable;
Run Code Online (Sandbox Code Playgroud)
请注意,基本类型也不能用作类型参数.