通用接口出现错误:接口 Observer 不能使用不同的参数多次实现:
我在编写 GWT 应用程序时在 Eclipse 中收到此错误
接口Observer不能 多次实施 不同的论点: 观察者
和 观察者
public class CompositeWordLists extends Composite implements Observer<DialogBoxAuthenticate>, Observer<CompositeListData>
这是界面
public interface Observer<T> {
public void update(T o);
}
是这样吗?如何解决这个问题,而不必为每个可能的事件创建大量的观察者类?
I am getting this error in Eclipse while writing a GWT app
The interface Observer cannot be
implemented more than once with
different arguments:
Observer<CompositeListData > and
Observer<DialogBoxAuthenticate>
public class CompositeWordLists extends Composite implements Observer<DialogBoxAuthenticate>, Observer<CompositeListData>
Here is the interface
public interface Observer<T> {
public void update(T o);
}
Is this right? How can I get around this problem without having to create a multitude of Observer classes for every possible event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于类型擦除,您无法两次实现相同的接口(使用不同的类型参数)。因此,您收到的 eclipse 错误是正确的。
您可以为所有可能的“T”添加基类,这可能会受到限制并且没有用,具体取决于这些类的范围。而且,您请求了一个解决方案,可以防止您为每个可能的事件创建大量观察者类(我假设是接口),我不知道您还能如何在不影响编译时安全的情况下做到这一点。
我建议如下
代码混乱并不可怕,如果将它们全部放在一个文件中,它们将很容易引用和维护。希望我能帮助
编辑:在谷歌上进行一番挖掘后(这让我回到了stackoverflow!),你的问题以不同的方式提出,并得到了类似的回答此处
Because of type erasure you can't implement the same interface twice (with different type parameters). So, the eclipse error that you are receiving is correct.
You could add a base class for all possible "T", which may be limiting and not useful depending on the scope of these classes. And, you have requested a solution that prevents you from creating a multitude of Observer classes (i am assuming interfaces) for every possible event, well I can't see how else you would do that without compromising compile time safety.
I would suggest the following
The code clutter isn't horrible and if you place them all in one file, they will be easy to reference and maintain. Hope I have helped
EDIT: After some digging around on google (which pointed me back to stackoverflow!, your question was asked in a different fashion and answered similarly here
Composite 必须已经实现 Observer。这是真正的意图吗?您希望这个 CompositeWordLists 类同时观察两种方式吗?
Composite must already implement Observer. Is that what really intended? You want this CompositeWordLists class to observe two ways at once?
不确定这是否有帮助,但我今天遇到了同样的 Java 编译错误。
我通过提取我可以获得的所有共享实现来部分解决我的问题:参数化抽象类:
Not sure this can help, but I came across the same Java compile error today.
I partially solved my case by extracting all the shared implementation I could get to a parametrized abstract class: