簡體   English   中英

編譯器要求重寫Collections中的可選方法

[英]Compiler asking for optional methods from Collections to be overridden

我正在與同學一起創建一個全班級項目,應該為某些函數創建請求請求,但是在創建類並以一種可以編譯的方式覆蓋方法時遇到了一些問題(我現在不需要編寫方法,這就是項目,我只需要編譯即可。 我發現大多數方法都可以工作(或者至少編譯器沒有抱怨),但是我對一些事情感到困惑:

  • 編譯器抱怨可選方法(例如set和addAll)

  • 方法addAll盡管已被添加,但抱怨它並未被覆蓋,盡管它被覆蓋,當我為其添加另一個addAll方法時,我也遇到了擦除錯誤。

我已經閱讀了很多,但是找不到有關如何解決它的正確結論。 我只是使用Atom編寫代碼,而使用Terminal編寫終端,沒有花哨的IDE(也許我應該學習一個)。

如果不清楚,我只是在尋找可用的方法存根,而不是每個方法的全面答案,因為這是帶有類的項目。

  // https://docs.oracle.com/javase/8/docs/api/java/util/List.html

import java.util.*;
import java.lang.reflect.*;

public class SkipList<E> implements List<E>
{
    // compiler complaining, then added, although optional
    public E set(int index, E element)
    {
        throw new IndexOutOfBoundsException();
    }

    // compiler complaining, then added, although optional
    public boolean addAll(Collection <? extends E> c)
    {
        return true;
    }

    // Group 1
    public boolean add(E e)
    {
        return true;
    }

    public void add(int index, E e)
    {

    }

    public boolean addAll(Collection c)
    {
        return true;
    }

    public int indexOf(Object o)
    {
        int index = 0;
        return index;
    }

    public int lastIndexOf(Object o)
    {
        int index = 0;
        return index;
    }

    // Group 2
    public boolean contains(Object o)
    {
        return true;
    }

    public boolean containsAll(Collection c)
    {
        return true;
    }

    public boolean equals(Object o)
    {
        return true;
    }


    public List<E> subList(int fromIndex, int toIndex)
    {
        List<E> sub = new SkipList<>();
        return sub;
    }

    // Group 3
    public boolean isEmpty()
    {
        return true;
    }

    public int size()
    {
        int size = 0;
        return size;
    }

    public void clear()
    {

    }

    public E get(int index)
    {
        throw new IndexOutOfBoundsException();
    }

    public E getQuantile(double quantile) // e.g. 0 = minimum, 0.5 = median, 1 = max
    {
        throw new IndexOutOfBoundsException();
    }

    // Group 4
    public Iterator<E> iterator()
    {
        throw new IndexOutOfBoundsException();
    }

    public ListIterator<E> listIterator()
    {
        throw new IndexOutOfBoundsException();
    }

    public ListIterator<E> listIterator(int index)
    {
        throw new IndexOutOfBoundsException();
    }

    // Group 5
    public E remove(int index)
    {
        throw new IndexOutOfBoundsException();
    }

    public boolean remove(Object o)
    {
        return true;
    }

    public boolean removeAll(Collection c)
    {
        return true;
    }

    public boolean retainAll(Collection c)
    {
        return true;
    }

    // Group 6
    public int hashCode()
    {
        int hashCode = 0;
        return hashCode;
    }

    public Object[] toArray()
    {
        Object[] arr = new Object[0];
        return arr;
    }

    public <T> T[] toArray(T[] a)
    {
        return a;
    }
}

事實證明,在讀取API時,由於某種原因,我掩蓋了addAll方法的另一個參數,這使我相信其他錯誤。 我更改了具有正確參數的方法,並對其進行了編譯。

public boolean addAll(int index, Collection <? extends E> c)
{
    return true;
}

暫無
暫無

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

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