我正在研究一组代表 SQL 结果集的抽象,并编写了以下方法:
public interface Column<T>{
//some methods
}
public class SqlRowExtractor{
public void doExtraction(){
Collection<Column<?>> cols = getColumns(); //Returns the collection of Column<T>.
//A particular result set may contain,
//for instance 2 Column<Date>,
//1 Column<Integer> and so fotrh
for(Column<?> c : cols){
put(c, get(c)); //1 <-------------------- HERE
//Compile-error
}
//other staff
}
public <T> void put(Column<T> c, T o){
//put the extracted value in another place
}
public <T> T get(Column<T> c) throws SQLException{
//extract the value of the row for the Column<T> c
}
}
我在 处得到了编译错误//1
,尽管通过put
和get
方法的签名非常清楚,没有办法放置不适当的值。如何以类型安全的方式修复错误?错误信息:
The method put(Column<T>, T) is not applicable for the arguments
(Column<capture#3-of ?>, capture#4-of ?)