想象一下有一个Java类。
此外,我该如何避免警告:“Class is a raw type. References to generic type Class should be parameterized”在FixedClassOsStrings中出现?声明以下内容是否可行:
如果可以,我应该使用泛型返回什么?
public class FixedClassOfStrings {
List<String> myMember=new ArrayList<String>();
// omissed all non relevant code...
public Class getMyType() {
return String.class;
}
}
如何使用Java泛型使其参数化?
我的尝试失败了:
public class GenericClass<T> {
List<T> myMember=new ArrayList<T>();
public Class getMyType() {
return T.class; // this gives "Illegal class literal for the type parameter T"
}
}
此外,我该如何避免警告:“Class is a raw type. References to generic type Class should be parameterized”在FixedClassOsStrings中出现?声明以下内容是否可行:
public Class<String> getMyType() {
return String.class;
}
...
如果可以,我应该使用泛型返回什么?
public Class<T> getMyType() {
return T.class; // this gives "Illegal class literal for the type parameter T"
}
...
非常感谢您的帮助和支持!
T.class
是一个编译时常量,但由于代码是泛型的,编译器无法确定返回哪个类对象。 - awksp