[英]Returning a generic Interface with concrete implementation in java
I have a generic Interface which is implemented by a class, that I want to return in a generic method. 我有一个由类实现的通用接口,我希望以泛型方法返回。 Interface: 接口:
public interface IWorker<T extends Object, K extends Object> {
public K doWork(T o);
}
Implementation: 执行:
public class WorkerImpl implements IWorker<String, List<Serializable>>
{
@Override
public List<Serializable> doWork(String s)
{
return ...
}
}
ActionCoordinator Interface for the generic method returning the implementation: ActionCoordinator返回实现的泛型方法的接口:
public interface IActionCoordinator
{
public <T extends Serializable, K extends Serializable> IWorker<T, K> getAction(T request);
}
ActionCoordinator implementation: ActionCoordinator实现:
public class ActionCoordinatorImpl implements IActionCoordinator
{
@Override
public <T extends Serializable, K extends Serializable> IWorker<T, K> getAction(final T requ)
{
return (IWorker<T,K>)new WorkerImpl();
}
}
Problem: In eclipse this will work, but doing a maven build with the JDK 1.6.0_35 doesn't and says "inconvertible types". 问题:在eclipse中这将起作用,但使用JDK 1.6.0_35进行maven构建却不会,并且说“不可逆转的类型”。 I can get around with that: 我可以解决这个问题:
public class ActionCoordinatorImpl implements IActionCoordinator
{
@Override
public <T extends Serializable, K extends Serializable> IWorker<T, K> getAction(final T requ)
{
Object temp = new WorkerImpl();
return (IWorker<T,K>)temp;
}
}
But that it's not supposed to be, that wouldn't be type-safe at all. 但它不应该是,那根本不是类型安全的。
Some ideas would be nice. 一些想法会很好。 Thanks in advance... 提前致谢...
EDIT: What works for me now is the following: I changed all the generic Ts and Ks to be Serializable. 编辑:现在对我有用的是:我将所有通用的Ts和Ks都改为Serializable。 Thats all what I needed to restrict the WorkerImpl to be. 这就是我需要限制WorkerImpl的所有东西。 the actual caller still needs an IWorker<T,K>
but IWorker<Serializable,Serializable>
fits and works...thanks everyone, but I still wonder why eclipse is not saying anything... 实际的调用者仍然需要IWorker<T,K>
但IWorker<Serializable,Serializable>
适合并且工作...感谢大家,但我仍然想知道为什么eclipse不会说什么...
The problem is not that maven won't compile, it's that Eclipse isn't complaining about an unsafe cast: You probably have an option turned off in your Eclipse preferences and are doing a strict compile in maven. 问题不在于maven 不会编译,而是Eclipse不抱怨不安全的演员:您可能在Eclipse首选项中关闭了一个选项,并且正在maven中进行严格编译。
Your WorkerImpl
is unsuitable to return from your factory method in ActionCoordinatorImpl
, because there's no guarantee that the Serializable
passed to its doWork()
method will be a String
- it just has to be any Serializable
. 您的WorkerImpl
不适合从ActionCoordinatorImpl
的工厂方法返回,因为无法保证传递给其doWork()
方法的Serializable
将是一个String
- 它只需要是任何Serializable
。
Also, you could simplify your code by changing IWorker
from 此外,您可以通过更改IWorker
来简化代码
public interface IWorker<T extends Object, K extends Object>
to 至
public interface IWorker<T, K>
Since they are equivalent (everything extends Object) 因为它们是等价的(一切都扩展为Object)
你是否正确设置了maven的源/目标 ?
Your WorkerImpl
only implements IWorker<String, List<Serializable>>
. 您的WorkerImpl
仅实现IWorker<String, List<Serializable>>
。 You then try to make it an IWorker<T extends Serializable, K extends Serializable>
in ActionCoordinatorImpl
which is not correct and should be reported as an error. 然后尝试将其作为IWorker<T extends Serializable, K extends Serializable>
in ActionCoordinatorImpl
,这是不正确的,应报告为错误。
It would be difficlt to recommend a fix until you are clear what you want to do. 在你清楚自己想要做什么之前,推荐一个修复是很困难的。
new WorkerImpl()
yields IWorker<String, List<Serializable>>
, how are you going to convert it to IWorker<T,K>
? new WorkerImpl()
产生IWorker<String, List<Serializable>>
,你打算如何将它转换为IWorker<T,K>
? And where do you think compiler should get the type K
? 你认为编译器应该在哪里得到K
型? Will you always pass it explicitly when invoking getAction
? 在调用getAction
时,您是否总是显式传递它? Then you have to pass type T
also, but it is also set by the parameter requ
, and the invocation would look ugly. 然后你也必须传递类型T
,但它也由参数requ
设置,并且调用看起来很难看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.