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

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

问题描述


接口Observer不能被
实现多次与
不同的参数:
Observer< CompositeListData>和
Observer< DialogBoxAuthenticate>



  public class CompositeWordLists extends Composite Observer< DialogBoxAuthenticate> ;,Observer< CompositeListData> 

以下是介面

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

这是正确的吗?如何解决这个问题,而不必为每个可能的事件创建大量的Observer类?

你不能两次实现相同的接口(使用不同的类型参数)。所以,你收到的eclipse错误是正确的。

您可以为所有可能的T添加一个基类,根据这些类的范围,这可能是有限制的并且没有用处。并且,您已经请求了一个解决方案,以防止您为每个可能的事件创建大量的Observer类(我假设有接口),以及我无法看到您将如何做,而不会影响编译时安全性。



我会建议如下:

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

interface DialogBoxAuthenticateObserver extends Observer< DialogBoxAuthenticate> {
}

代码混乱并不可怕,如果将它们全部放在一个文件中,它们将很容易引用和维护。希望我已经帮助了

编辑:经过一番对谷歌的挖掘(这指出我回到了stackoverflow!时尚和回答类似这里


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?

解决方案

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

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

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

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

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