在Java中,我发现了类似的奇怪方法声明:
<T> T org.apache.commons.dbutils.BeanProcessor.toBean(ResultSet rs, Class<T> type) throws SQLException
这个<T> T
是什么意思?在<T>
之前T
的含义是什么,以及为什么返回的类型不仅仅是"T"
?
我已经下载了org.apache.commons.dbutils.BeanProcessor
类的源代码,并找到了以下方法:
private <T> T createBean(ResultSet rs, Class<T> type,
PropertyDescriptor[] props, int[] columnToProperty)
throws SQLException {
T bean = this.newInstance(type);
for (int i = 1; i < columnToProperty.length; i++) {
if (columnToProperty[i] == PROPERTY_NOT_FOUND) {
continue;
}
PropertyDescriptor prop = props[columnToProperty[i]];
Class<?> propType = prop.getPropertyType();
Object value = null;
if(propType != null) {
value = this.processColumn(rs, i, propType);
if (value == null && propType.isPrimitive()) {
value = primitiveDefaults.get(propType);
}
}
this.callSetter(bean, prop, value);
}
return bean;
}
如您所见,返回的bean
对象的T
类型不是<T> T
。为什么呢?