我有一个界面如下,
public interface MethodExecutor {
<T> List<T> execute(List<?> facts, Class<T> type) throws Exception;
}
另外,我有一个如下的通用实现,
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;
}
}
到目前为止,没有编译问题,
但该接口的具体实现无法编译,如下所示。
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;
}
}
如何为某些已定义的对象实现此接口?我需要使用类级别的泛型吗?
你需要做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
{
//...
}
}
如果元素类型为facts
应该可以针对特定实现进行配置,您也需要将其设置为参数。
public interface MethodExecutor<T, F> {
List<T> execute(List<? extends F> facts, Class<T> type) throws Exception;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)