我有一个如下界面,
public interface MethodExecutor {
<T> List<T> execute(List<?> facts, Class<T> type) throws Exception;
}
Run Code Online (Sandbox Code Playgroud)
另外,我有一个如下的通用实现,
public class DefaultMetodExecutor implements MethodExecutor {
public <T> List<T> execute(List<?> facts, Class<T> type) throws Exception
{
List<T> result = null;
//some implementation
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
至此,没有编译问题,
但是这个接口的具体实现无法编译,如下所示.
public class SpecificMetodExecutor implements MethodExecutor {
public <Model1> List<Model1> execute(List<Model2> facts, Class<Model1> type) throws Exception
{
List<Model1> result = null;
//some implementation specific to Model1 and Model2
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
如何为某些已定义的对象实现此接口?我需要去上课级别的泛型吗?
您需要创建T类类型参数,而不是方法类型参数.您不能使用非泛型方法覆盖泛型方法.
public interface MethodExecutor<T> {
List<T> execute(List<?> facts, Class<T> type) throws Exception;
}
public class DefaultMethodExecutor implements MethodExecutor<Model1> {
public List<Model1> execute(List<?> facts, Class<Model1> type) throws Exception
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
如果facts要为特定实现配置元素类型,则还需要将其作为参数.
public interface MethodExecutor<T, F> {
List<T> execute(List<? extends F> facts, Class<T> type) throws Exception;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19561 次 |
| 最近记录: |