【发布时间】:2013-06-18 16:19:15
【问题描述】:
我正在尝试构建一个实现Queue 和Map 的类。两个接口都定义了remove(Object) 方法,但返回类型不同:
public interface Collection<E> { //Queue extends Collection, which has the problem method
public boolean remove(Object e);
//...
}
public interface Map<K,V> {
public V remove(K key);
//...
}
public class QueuedMap<K,V> extends AbstractMap implements Queue {
public V remove(K key) {/* ... */}
//ERROR: V is not compatible with boolean
//...
}
K 的类型擦除导致这两个方法签名发生冲突。我不能拥有其中一个,因为它是无效的覆盖,我不能同时拥有两者,因为它们具有相同的签名。有什么办法可以让这两个接口共存?
【问题讨论】:
-
即兴发挥,对我来说似乎不可能。
-
如果您调用 remove(key),您的班级将如何决定使用哪一个?
标签: java inheritance interface type-erasure