I've the following classes
KeyValue.java
package test;
public class KeyValue<T> {
private String key;
private T value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
Reader.java
package test;
public interface Reader<T> {
<S extends T> S read(Class<S> clazz);
}
Test.java
package test;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<KeyValue<Object>> list = find(KeyValue.class, new Reader<KeyValue<Object>>() {
@Override
public <S extends KeyValue<Object>> S read(Class<S> clazz) {
return null;
}
});
}
public static <T> List<T> find(Class<T> targetClass, Reader<T> reader) {
return null;
}
}
这里的方法调用 find(......)
在编译时失败,出现错误信息:
类型Test中的方法find(Class, Reader)对于参数(Class, new Reader>() {})不适用。
该方法必须返回类型为 List<KeyValue<Object>>
的对象。
这个设计有什么问题,如何修复。
谢谢。
KeyValue
类型。 - Arun P JohnyThe method find(Class<T>, Reader<T>) in the type Test is not applicable for the arguments (Class<KeyValue>, new Reader<KeyValue<Object>>(){})
。 - Arun P Johny<KeyValue>
,将第二个T解释为<KeyValue<Object>>
,因此它们不相同--也许应该更改您的签名为find(Class<X extends T> targetClass, Reader<T> reader)
或find(Class<T> targetClass, Reader<X super T> reader)
,具体取决于您想要表达的内容(我假设是第一个)。 - BertNase