java中如果一个类的父类是ServiceReturn,而这个类的每个方法的返回类型也是这个类,这样我们应该怎么理解?
本帖最后由 Henry_YQH 于 2012-11-12 20:53:53 编辑
java中如果一个类的父类是ServiceReturn,而这个类的每个方法的返回类型也是这个类,这样我们应该怎么理解?
比如说我们最近的一个项目,其中的类:
public class ServiceReturns<T> {
private boolean success ;
private String messageCode;
private T dataObject;
private String forwardUrl="";
....
}
public class BaseService<T> extends ServiceReturns<T>{
private LogDetails logDetails = new LogDetails();
protected LogDao logDao;
public void saveLog(LogDetails log){
logDao.save(log);
}
public LogDao getLogDao() {
return logDao;
}
public void setLogDao(LogDao logDao) {
this.logDao = logDao;
}
public LogDetails getLogDetails() {
return logDetails;
}
public void setLogDetails(LogDetails logDetails) {
this.logDetails = logDetails;
}
public String getClassName(){
return this.getClass().getName();
}
protected void saveLog(Exception exception, LogDetails logDetails, String messageCode){
this.logDetails = logDetails;
this.logDetails.setOccurTime(new Date());
this.logDetails.setDetail(exception.getCause().getMessage());
this.saveLog(this.logDetails);
this.setMessageCode(MessageConstant.SAVE_LOG_FAILURE);
}
}
public class UserService extends BaseService {
private IUserDao userDao;
public ServiceReturns<User> login(User user) throws Exception {
.......
return this;
}
public ServiceReturns<User> save(User user) throws Exception {
.....
}
public ServiceReturns delete(List<User> userList) throws Exception {
........
return this;
}
public ServiceReturns<User> findById(Integer userId) throws Exception {
}
}
应该怎么理解呢,我感觉好麻烦啊??还有每次的return this,是不是多余啊,在每个方法的每个验证里面都写return,最后可以不用写return this了吧?
------最佳解决方案--------------------只说说明你所有的BaseService都是ServiceReturns
BaseService具有ServiceReturns的特征。