我有一个将对象转换为字符串的接口:
public interface Converter<T> {
String asString(T object);
}
以及存储所有可用转换器的地图:
Map<Class<?>, Converter<?>> converterMap;
现在我有一个异构数据列表,可以像这样转换:
List<?> data = fetchData();
List<String> stringData = new ArrayList<>(data.size());
for (Object datum : data) {
stringData.add(convertrMap.get(datum.getClass()).asString(datum));
}
但是这段代码不能编译:
error: method asString in interface Converter<T> cannot be applied to given types;
stringData.add(converterMap.get(datum.getClass()).asString(datum));
required: CAP#1
found: Object
reason: actual argument Object cannot be converted to CAP#1 by method invocation conversion
where T is a type-variable:
T extends Object declared in interface Converter
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
我应该如何更改代码?