0

我的情况是:保存方法ResourceService<T , ID >

public interface IResourceService<T,ID>

public class ResourceService<T , ID> implement IResourceService<T,ID>
{
   public void save(T entity) throws RuntimeException {

    try {
        AsyncServiceSaveRunable<T> task = new
AsyncServiceSaveRunable<T>(getService(),entity);

        this.treadPoolExcutor.submitTask(task);
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


   }
}

并且在

public class AsyncServiceSaveRunable<T> implements Runnable  {
private IResourceService<?,?> service; //----> this is 
private Method serviceMethod;
private List<T> parameters;

public AsyncServiceSaveRunable(IResourceService<?,?> service,  List<T> parameters){ 
}

public AsyncServiceSaveRunable(IResourceService<?,?> service, T parameter)
                            throws NoSuchMethodException, SecurityException{

    this.service         = service;

    this.parameters = new ArrayList<T>();
    this.parameters.add(parameter);
}
@Override
public void run() {
    try {
        if(this.parameters.size()>1)
            service.saveList(this.parameters);
        else
            service.save( this.parameters.get(0));
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

  }
 }

问题是 service.saveList(this.parameters); 和 service.save(...); 错了

无论如何要传递该参数还是我需要不同的结构来处理这种情况?

谢谢

------------------ 来自 Eclipse 的错误消息 ------------------------

The method save(capture#7-of ?) in the type IResourceService<capture#7-of ?,capture#8-of ?>
 is not applicable for the arguments (T)
4

1 回答 1

4

ResourceService<T extends BaseEntity, ...>你需要T被驱赶BaseEntity

public class AsyncServiceSaveRunable<T> T没有限制。

然后,service.save(this.parameters.get(0));您尝试将(无界)泛型类型传递给需要BaseEntity对象(或从它派生的对象)的函数。

AsyncServiceSaveRunable您也可以要求从 BaseEntity 派生的类型参数:

public class AsyncServiceSaveRunable<T extends BaseEntity>

更新:错误消息是第一个类型参数对于 .IResourceService所需的类型无效save()。以前您的类型参数以 为界BaseEntry,但现在缺少此限制。也许你想写:IResourceService<? extends BaseEntity, ? extends Serializable>

于 2012-04-16T05:52:28.750 回答