我在编译代码时遇到一些问题。
$ mvn clean compile
出现此类错误时请通知我。
[58,30] type parameters of <D,K>D cannot be determined; no unique maximal instance exists for type variable D with upper bounds DS,
也许这个问题是由泛型类型的递归边界
引起的。对吗?
引用文献: Generics compiles and runs in Eclipse, but doesn't compile in javac
我该如何解决这个问题?
@SuppressWarnings("unchecked")
public static <D extends DataStore<K, T>, K, T extends Persistent> D createDataStore(Class<T> persistent, Properties properties) throws IOException {
try {
return (D) (new HBaseStore<K, T>());
} catch (Exception e) {
throw new RuntimeException("cannot initialize a datastore", e);
}
}
public static <DS extends DataStore<U, P>, U, P extends Persistent> DS createDataStore(Class<P> persistent) throws IOException {
return createDataStore(persistent, null); // ERROR
}
最佳答案
这意味着输入 D
仅作为通用参数存在。例如T
可以从方法参数 Class<T> persistent
解析。如果将方法的签名更改为类似以下内容,则可以解决此问题:
public static <D extends DataStore<K, T>, K, T extends Persistent> D createDataStore(Class<T> persistent, Class<D> dataStoreType, Properties properties)
关于java - 如何解决一般递归约束问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12910053/