Java OrmLite泛型创建方法
我想为扩展抽象类Java OrmLite泛型创建方法,java,generics,ormlite,Java,Generics,Ormlite,我想为扩展抽象类BusinessObject的每个BO包含一个create方法。我提出了如下所示的方法,但在编译时,我在createDao(…)语句中得到了以下异常: 'invalid inferred types for D; inferred type does not conform to declared bound(s)' 这就是create方法的外观 public abstract class BusinessObject { ... public <T
BusinessObject
的每个BO包含一个create方法。我提出了如下所示的方法,但在编译时,我在createDao(…)
语句中得到了以下异常:
'invalid inferred types for D; inferred type does not conform to declared bound(s)'
这就是create方法的外观
public abstract class BusinessObject {
...
public <T extends BusinessObject> T create(T bo) throws InstantiationException, SQLException,
NamingException {
final Dao<T, Long> dao = DaoManager.createDao(getConnectionSource(), bo.getClass());
bo.setTimestampAdded(System.currentTimeMillis());
bo.setTimestampModified(System.currentTimeMillis());
bo.setDateAdded(new Date(System.currentTimeMillis()));
bo.setDateModified(new Date(System.currentTimeMillis()));
dao.create(bo);
return bo;
}
}
公共抽象类BusinessObject{
...
公共T创建(T bo)抛出实例化异常、SQLException、,
NamingException{
final Dao=DaoManager.createDao(getConnectionSource(),bo.getClass());
添加了bo.settimestamps(System.currentTimeMillis());
setTimestampModified(System.currentTimeMillis());
添加了bo.setdate(新日期(System.currentTimeMillis());
bo.setDateModified(新日期(System.currentTimeMillis());
创建(bo);
返回bo;
}
}
在这个主题上,也许有人可以把我和一个关于泛型的像样的、全面的教程联系起来。有很多基本的教程,但中间的东西不多。好的,我已经玩了一些,发现更改方法签名以包含推断类是关键
public <T extends BusinessObject> T create(T bo, Class<T> cls) throws InstantiationException,
SQLException, NamingException {
final Dao<T, Long> dao = DaoManager.createDao(getConnectionSource(), cls);
bo.setTimestampAdded(System.currentTimeMillis());
bo.setTimestampModified(System.currentTimeMillis());
bo.setDateAdded(new Date(System.currentTimeMillis()));
bo.setDateModified(new Date(System.currentTimeMillis()));
dao.create(bo);
return bo;
}
public T create(T bo,Class cls)抛出实例化异常,
SQLException,NamingException{
final Dao=DaoManager.createDao(getConnectionSource(),cls);
添加了bo.settimestamps(System.currentTimeMillis());
setTimestampModified(System.currentTimeMillis());
添加了bo.setdate(新日期(System.currentTimeMillis());
bo.setDateModified(新日期(System.currentTimeMillis());
创建(bo);
返回bo;
}
原因是getClass()
返回Class
,而createDao(…)
期望Class
。我想知道是否可以将Class
转换为Class
。然而,Eclipse发出了一个警告:
'类型安全性:未选中从一个类到另一个类的强制转换'