【发布时间】:2010-12-12 07:00:28
【问题描述】:
考虑以下示例:
public class Sandbox {
public interface Listener<T extends JComponent> {
public void onEvent(T event);
}
public interface AnotherInterface extends Listener<JPanel>, Listener<JLabel> {
}
}
这会失败并出现以下错误
/media/PQ-WDFILES/programming/Sandbox/src/Sandbox.java:20: Sandbox.Listener cannot be inherited with different arguments: <javax.swing.JPanel> and <javax.swing.JLabel>
public interface AnotherInterface extends Listener<JPanel>, Listener<JLabel> {
^
1 error
为什么?生成的方法没有重叠。事实上,这基本上意味着
public interface AnotherInterface {
public void onEvent(JPanel event);
public void onEvent(JLabel event);
}
那里没有重叠。那么为什么会失败呢?
如果您想知道我在做什么并有更好的解决方案:我有一堆事件和一个监听器接口,几乎与上面的 Listener 类完全相同。我想创建一个适配器和一个适配器接口,为此我需要使用特定事件扩展所有侦听器接口。这可能吗?有没有更好的方法来做到这一点?
【问题讨论】: