Java – can’t contain the same interface as different parameters?
•
Java
Consider the following example:
public class Sand@R_886_2419@ { public interface Listener<T extends JComponent> { public void onEvent(T event); } public interface AnotherInterface extends Listener<JPanel>,Listener<JLabel> { } }
This will fail with the following error
/media/PQ-WDFILES/programming/Sand@R_886_2419@/src/Sand@R_886_2419@.java:20: Sand@R_886_2419@.Listener cannot be inherited with different arguments: <javax.swing.JPanel> and <javax.swing.JLabel> public interface AnotherInterface extends Listener<JPanel>,Listener<JLabel> { ^ 1 error
Why? The generated methods do not overlap In fact, this basically means
public interface AnotherInterface { public void onEvent(JPanel event); public void onEvent(JLabel event); }
No overlap Then why did it fail?
If you want to know what I'm doing and have a better solution: I have a bunch of events and a listener interface, almost exactly like the listener class above I want to create an adapter and adapter interface, so I need to extend all listener interfaces with a specific event Is that possible? Is there a better way to do this?
Solution
No, you can't
public interface AnotherInterface { public void onEvent(List<JPanel> event); public void onEvent(List<JLabel> event); }
Or implement the interface with several parameters
UPD
I think the solution will be as follows:
public class Sand@R_886_2419@ { // .... public final class JPanelEventHandler implements Listener<JPanel> { AnotherInterface target; JPanelEventHandler(AnotherInterface target){this.target = target;} public final void onEvent(JPanel event){ target.onEvent(event); } } ///same with JLabel }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码