获取通用接口错误:接口观察者不能用不同的参数多次实现: [英] Getting error for generic interface: The interface Observer cannot be implemented more than once with different arguments:

查看:16
本文介绍了获取通用接口错误:接口观察者不能用不同的参数多次实现:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Eclipse 中编写 GWT 应用程序时遇到此错误

I am getting this error in Eclipse while writing a GWT app

接口观察者不能实施了不止一次不同的论点:观察者<CompositeListData>和观察者

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>

这是界面

public interface Observer<T> {
    public void update(T o);
}

这样对吗?如何在不必为每个可能的事件创建大量 Observer 类的情况下解决这个问题?

Is this right? How can I get around this problem without having to create a multitude of Observer classes for every possible event?

推荐答案

由于类型擦除,您不能两次实现相同的接口(使用不同的类型参数).因此,您收到的 eclipse 错误是正确的.

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.

您可以为所有可能的T"添加一个基类,根据这些类的范围,这可能是限制性的并且没有用.而且,您已经请求了一个解决方案,该解决方案可以防止您为每个可能的事件创建大量 Observer 类(我假设是接口),好吧,我不知道您还能如何在不影响编译时安全的情况下做到这一点.

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.

我建议如下

interface Observer<T>{
    public void update (T o);
}

interface DialogBoxAuthenticateObserver extends Observer<DialogBoxAuthenticate>{
}

代码混乱并不可怕,如果您将它们全部放在一个文件中,它们将易于参考和维护.希望我有所帮助

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

编辑:在谷歌上进行了一些挖掘(这让我回到了stackoverflow!/questions/1297972/how-to-make-a-java-class-that-implements-one-interface-with-two-generic-types">这里

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

这篇关于获取通用接口错误:接口观察者不能用不同的参数多次实现:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆