实现 Iterator 的抽象实例 class 不会迭代

Instance of abstract class that implements Iterator won't iterate

似乎当我有一个实现接口的抽象 class 时,接口没有被扩展我的抽象 class 的主要 class 获取:

当我使用这个时:

public abstract class MusicIntCollection implements Iterator<Integer>{...}

我无法迭代 StepIndexCollection 的实例:

public class StepIndexCollection extends MusicIntCollection{...}

然而,当我创建这个时:

public class StepIndexCollection extends MusicIntCollection implements Iterator<Integer>{...}

...我可以迭代 StepIndexCollection 的一个实例。

以下是我的迭代方式:

this.notes = new StepIndexCollection();
    for (int i : o.notes()) {
        this.notes.add(i);
    }

有没有办法让它从抽象 class 开始工作,这样我扩展 MusicIntCOllection 的所有特定 class 类型也不必实现该接口?

您正在实现 Iterator interface, not the Iterable 接口。如果您使 class 实现 Iterable,您将能够使用增强的 for 循环语法。