小编Mar*_*enn的帖子

使用泛型类型实现接口

我正在尝试实现Spring的RowMapper接口,但是,我的IDE提示我将返回对象强制转换为"T",我不明白为什么.谁能解释我错过的东西?

public class UserMapper<T> implements RowMapper<T> {
    public T mapRow(ResultSet rs, int row) throws SQLException {
        User user = new User();
        user.firstName(rs.getInt("fname"));
        user.lastName(rs.getFloat("lname"));
        return user; // Why am I being prompted to cast this to "T", should this be fine?
    }
}
Run Code Online (Sandbox Code Playgroud)

java generics

4
推荐指数
1
解决办法
4899
查看次数

标签 统计

generics ×1

java ×1