我有一个java分配,我应该构造一个像Multiset一样工作的类。该类必须实现接口集合。我试图做到这一点,并声明集合中的所有方法(找到here)。但是,当我编译此代码时,出现以下错误:实现java.util.Collection <E>和覆盖方法的问题
error: TreeMultisetNy is not abstract and does not override abstract method retainAll(Collection<?>) in Collection
为什么会发生这种情况?
继承人我的代码:
import java.util.*;
public class TreeMultisetNy<E extends Comparable<E>> implements Collection<E> {
private Map<E, Integer> data = new TreeMap<E, Integer >();
public boolean add(E ny) {
return true;
}
public boolean addAll(Collection<? extends E> c){
return false;
}
public void clear() {
}
public boolean contains(E what) {
return false;
}
public boolean containsAll(Collection<?> c) {
return false;
}
public boolean equals(E what) {
return false;
}
public int hashCode() {
return 0;
}
public boolean isEmpty() {
return false;
}
public Iterator<E> iterator() {
return null;
}
public boolean remove(E what) {
return false;
}
public boolean removeAll(Collection<?> c) {
return false;
}
public boolean retainAll(Collection<?> c) {
return false;
}
public int size() {
return 0;
}
public Object[] toArray() {
return null;
}
public Object[] toArray(Object[] a){
return null;
}
}
我发现这个问题: How to create a class that implements java.util.collections 但我不相信,我犯同样的错误的那个家伙,还是我错了?
请给我一些提示,我一直在编码php多年,但面向对象对我来说是新的!
尝试使用方法签名'public boolean retainAll(Collection > c)'而不是'public boolean retainAll(Collection c)'。 –
2013-04-07 16:51:59
和'public boolean addAll(Collection extends E> c)'。此外,请看一下AbstractCollection - 扩展而不是自己实现整个接口可能会节省一些工作量。 – Pyranja 2013-04-07 16:54:05
他们所说的'不抽象'是因为你的类不是抽象的,它必须实现所有的方法。 – flup 2013-04-07 16:55:27