开发者

generate object via generic type in java

开发者 https://www.devze.com 2023-02-21 18:02 出处:网络
i\' ve a code snippet like below. but it gives error at list.add(mapp开发者_如何学编程er.mapRow()); line says \"The method add(K) in the type List is not applicable for the arguments (Object)\".

i' ve a code snippet like below. but it gives error at list.add(mapp开发者_如何学编程er.mapRow()); line says "The method add(K) in the type List is not applicable for the arguments (Object)". how can i fix it?

thanks.

    public List<K>  fetchData(JStarRowMapper mapper) {
    List<K> list = new ArrayList<K>();
    list.add(mapper.mapRow());
    return list;
}




public class IncomingRowMapper<K> implements JStarRowMapper {
@Override
public IncomingVO mapRow(ResultSet rs) throws SQLException {

    IncomingVO vo = new IncomingVO();
    vo.setId(rs.getInt("id"));
    vo.setUsername(rs.getString("username"));
    vo.setProcessDate(rs.getTimestamp("process_date"));
    vo.setProcessCount(rs.getInt("process_count"));
    return vo;
}

}

public interface JStarRowMapper<K> {
abstract public K mapRow(ResultSet rs) throws SQLException;

}


In the first snippet you should declare mapper as

JStarRowMapper<K>

Also mapRow is called without parameters, whereas it needs a ResultSet parameter


Another problem is that IncomingRowMapper should be declared as:

public class IncomingRowMapper implements JStarRowMapper<IncomingVO> { ...
0

精彩评论

暂无评论...
验证码 换一张
取 消