簡體   English   中英

實現java.util.Collection時遇到問題 <E> 和覆蓋方法

[英]Problems implementing java.util.Collection<E> and overriding methods

我有一個Java作業,應該構造一個像Multiset一樣的類。 該類必須實現接口Collections。 我嘗試這樣做,並聲明了Collections中的所有方法(在此處找到)。 但是,當我編譯此代碼時,出現以下錯誤:

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; 
}
}

我發現了這個問題: 如何創建一個實現java.util.collections的類,但是我不認為自己犯了與那個家伙相同的錯誤,還是我錯了?

請給我一些提示,我多年來一直在編寫php,但是面向對象對我來說是新的!

All ”方法的正確簽名如下:

boolean addAll(Collection<? extends E> c)
boolean containsAll(Collection<?> c)
boolean removeAll(Collection<?> c)
boolean retainAll(Collection<?> c)

您需要相應地更改班級。

另外, toArray()的一個參數版本的簽名是錯誤的。 它應該是

public <T> T[] toArray(T[] a)

同一行還有其他錯誤。 您需要仔細檢查您的類,確保每個方法都具有正確的簽名。

有關詳細信息,請參見Javadoc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM