我必须返回实现类之一作为接口的返回类型。
public class GenericReturnType {
public static <T extends Interface2> T resolveclazz(boolean isI3,Class<T>i3Clazz,Class<T>i4Clazz) throws IllegalaccessException,InstantiationException {
if(isI3) {
return i3Clazz.newInstance();
}
else {
return i4Clazz.newInstance();
}
}
}
但是当我将此方法称为
时GenericReturnType.resolveclazz(true,Interface3Impl.class,Interface4Impl.class);
我得到一个编译时错误
error: method resolveclazz in class GenericReturnType cannot be applied to given types;
Interface3 i3 = GenericReturnType.resolveclazz(true,Interface4Impl.class);
^
required: boolean,Class<T>,Class<T>
found: boolean,Class<Interface3Impl>,Class<Interface4Impl>
reason: inferred type does not conform to equality constraint(s)
inferred: Interface4Impl
equality constraints(s): Interface4Impl,Interface3Impl
where T is a type-variable:
T extends Interface2 declared in method <T>resolveclazz(boolean,Class<T>)
这是我所拥有的接口的结构。 Interface structures
有人可以帮我解决我在这里做错的事吗