Intellij 给出了奇怪的错误。 Java 泛型问题
Intellij is giving strange error. Java Generics issue
我想从字符串生成一个对象,我还希望生成的对象是扩展 IObject 的 IObjectImpl 类型。
所以,我有一个工厂 class 方法,它接受一个字符串和一个接口 class(比如扩展 IObject 的 IObjectImpl.class,这是强制性的)。该方法应自动检测从字符串(使用反射)类型生成的对象是 IObject 并将其转换为 IObjectImpl。
为了测试,我写了下面的代码。但是,Intellij 没有显示错误,同时在我 运行 main 方法时,我得到了最后显示的错误。
public <T extends IObject, E extends T> E getInstanceOfType(String clazz, Class type) {
try {
System.out.println("Type got is " + type);
return null;
} catch (Exception exception) {
throw new ObjectInstantiationException(String.format("Could not create the "
+ "instance of type %s", clazz), exception);
}
}
public static void main(String[] args) {
new Factory().getInstanceOfType("Some class", IObjectImpl.class);
}
错误是:
Error:(67, 49) java: ..path\Factory.java:67: incompatible types; inferred type argument(s) com.myCompany.IObject,java.lang.Object do not conform to bounds of type variable(s) T,E
found : <T,E>E
required: java.lang.Object
至于检查类型,我只知道 eClass.isAssignableFrom(tClass) 方法。
我的最终目标是我应该能够调用 IObjectImpl
中定义的方法而无需任何转换。我如何使用 Java 1.6 来做到这一点?
您可以这样指定类型:(java 6)
public static void main(String[] args) {
new Factory().<IObject, IObjectImpl> getInstanceOfType("Some class", IObjectImpl.class);
}
public class Factory {
public <T extends IObject, E extends T> E getInstanceOfType(String clazz, Class<E> type) {
try {
System.out.println("Type got is " + type);
return null;
} catch (Exception exception) {
throw new RuntimeException(String.format("Could not create the "
+ "instance of type %s", clazz), exception);
}
}
public static void main(String[] args) {
new Factory().<IObject, IObjectImpl>getInstanceOfType("Some class", IObjectImpl.class);
}
}
我想从字符串生成一个对象,我还希望生成的对象是扩展 IObject 的 IObjectImpl 类型。
所以,我有一个工厂 class 方法,它接受一个字符串和一个接口 class(比如扩展 IObject 的 IObjectImpl.class,这是强制性的)。该方法应自动检测从字符串(使用反射)类型生成的对象是 IObject 并将其转换为 IObjectImpl。
为了测试,我写了下面的代码。但是,Intellij 没有显示错误,同时在我 运行 main 方法时,我得到了最后显示的错误。
public <T extends IObject, E extends T> E getInstanceOfType(String clazz, Class type) {
try {
System.out.println("Type got is " + type);
return null;
} catch (Exception exception) {
throw new ObjectInstantiationException(String.format("Could not create the "
+ "instance of type %s", clazz), exception);
}
}
public static void main(String[] args) {
new Factory().getInstanceOfType("Some class", IObjectImpl.class);
}
错误是:
Error:(67, 49) java: ..path\Factory.java:67: incompatible types; inferred type argument(s) com.myCompany.IObject,java.lang.Object do not conform to bounds of type variable(s) T,E
found : <T,E>E
required: java.lang.Object
至于检查类型,我只知道 eClass.isAssignableFrom(tClass) 方法。
我的最终目标是我应该能够调用 IObjectImpl
中定义的方法而无需任何转换。我如何使用 Java 1.6 来做到这一点?
您可以这样指定类型:(java 6)
public static void main(String[] args) {
new Factory().<IObject, IObjectImpl> getInstanceOfType("Some class", IObjectImpl.class);
}
public class Factory {
public <T extends IObject, E extends T> E getInstanceOfType(String clazz, Class<E> type) {
try {
System.out.println("Type got is " + type);
return null;
} catch (Exception exception) {
throw new RuntimeException(String.format("Could not create the "
+ "instance of type %s", clazz), exception);
}
}
public static void main(String[] args) {
new Factory().<IObject, IObjectImpl>getInstanceOfType("Some class", IObjectImpl.class);
}
}