Java错误:x不是抽象的,并且不会在Iterable中覆盖抽象方法iterator()

问题描述 投票:-1回答:1

我正在尝试在泛型工作中实现Iterable,但我不断收到错误:

OptimizedLog is not abstract and does not override abstract method iterator() in Iterable

我不明白为什么会收到此错误,我的代码是:

public class OptimizedLog<E> implements Iterable<ElementOfLog>


public class LogIterator implements Iterator<ElementOfLog>
{
    private ElementOfLog currentElement;

    public LogIterator(ElementOfLog headOfStack)
    {
        currentElement = headOfStack;
    }

    public boolean hasNext()
    {
        if(currentElement.next == null)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public ElementOfLog next()
    {
        ElementOfLog tempEoL = currentElement;
        currentElement = currentElement.next;
        return tempEoL;
    }


    public void remove()
    {
        System.out.println("Remove is not allowed");
    }
}
java iterable
1个回答
0
投票
因为在Java中实现接口时,必须重写该接口内的所有方法,否则您的类将变为“抽象”,这意味着某些方法已声明但未实现,因此您的类无法例如被实例化,如文档https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html所示itearator接口必须实现

default void forEachRemaining(Consumer<? super E> action) boolean hasNext() E next() default void remove()

© www.soinside.com 2019 - 2024. All rights reserved.