通用接口出现错误:接口 Observer 不能使用不同的参数多次实现:

发布于 2024-10-05 06:06:06 字数 513 浏览 11 评论 0原文

我在编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她如夕阳 2024-10-12 06:06:06

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

您可以为所有可能的“T”添加基类,这可能会受到限制并且没有用,具体取决于这些类的范围。而且,您请求了一个解决方案,可以防止您为每个可能的事件创建大量观察者类(我假设是接口),我不知道您还能如何在不影响编译时安全的情况下做到这一点。

我建议如下

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

interface DialogBoxAuthenticateObserver extends Observer<DialogBoxAuthenticate>{
}

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

编辑:在谷歌上进行一番挖掘后(这让我回到了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

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

成熟的代价 2024-10-12 06:06:06

Composite 必须已经实现 Observer。这是真正的意图吗?您希望这个 CompositeWordLists 类同时观察两种方式吗?

Composite must already implement Observer. Is that what really intended? You want this CompositeWordLists class to observe two ways at once?

孤独患者 2024-10-12 06:06:06

不确定这是否有帮助,但我今天遇到了同样的 Java 编译错误。

我通过提取我可以获得的所有共享实现来部分解决我的问题:参数化抽象类

public enum Example {
    ;
    static interface Observer<T> { public void update (T o); }
    static abstract AbstractObserver<T> implements Observer<T> {
        public void update (T o) { /* shared stuff I can put here */ }
    }
    static Composite extends AbstractObserver<Parameter1> {
        public void update (T o) {
            super(o);
            /* type-specific code here */
        }
    }
    static CompositeWordLists extends AbstractObserver<Parameter2> {
        public void update (T o) {
            super(o);
            /* type-specific code here */
        }
    }
}

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:

public enum Example {
    ;
    static interface Observer<T> { public void update (T o); }
    static abstract AbstractObserver<T> implements Observer<T> {
        public void update (T o) { /* shared stuff I can put here */ }
    }
    static Composite extends AbstractObserver<Parameter1> {
        public void update (T o) {
            super(o);
            /* type-specific code here */
        }
    }
    static CompositeWordLists extends AbstractObserver<Parameter2> {
        public void update (T o) {
            super(o);
            /* type-specific code here */
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文