Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

java - Getting error for generic interface: The interface Observer cannot be implemented more than once with different arguments:

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...