泛型用在类跟方法上的写法
泛型用在类和方法上的写法
如果是用在 类上 public abstract class AbstractBatchBaseDao<T> 把 <T> 加在 类名上, 说明 后面 就定义了要用这个类型 比如它的子类像 public class BatchAgentProfitDaoImpl extends AbstractBatchBaseDao<AgentProfitRecord>{ ... } 比如在 AbstractBatchBaseDao 里面 放了 一个方法 protected void doBatch(String sql, List<T> beanList){ ... setParameter(ps,bean); 放了这个方法, 说明必须子类实现这个方法 ... } 所以在实现类 BatchAgentProfitDaoImpl 也有这个方法 @Override public void setParameter(PreparedStatement ps, AgentProfitRecord agentProfitRecord) { ps.setLong(1, agentProfitRecord.getBATID()); } 而如果使用 泛型的方法, 就会更加灵活, paramable是一个接口, 实现交给客户端来实现 取代之前必须要从自身实现一个 setParameter的方法 , protected <U> void doBatch(String sql, List<U> beanList, JDBCParamable<U> paramable){ paramable.setParamter(ps, bean) } 就是用接口而不是抽象类 的一个例子