该声明是有效的,但在该方法的上下文中是多余的。DataStructure.DataStructureIterator iterator = DataStructure.this.new EvenIterator();
请考虑此方案,其中存在冲突的内部类
public void printEven() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
//Makes EvenIterator point to DataStructure's implementation
DataStructureIterator itr = DataStructure.this.new EvenIterator();
}
class EvenIterator implements DataStructureIterator {
@Override
public boolean hasNext() {
return false;
}
@Override
public Integer next() {
return null;
}
@Override
public void remove() {
}
}
});
}
如您所见,匿名类具有名为的内部类(与 Outer 类的内部类同名)。所以写作只是Runnable
EvenIterator
DataStructureIterator itr = this.new EvenIterator(); //OR DataStructureIterator itr = new EvenIterator();
将引用 的 。要指向 的 ,您可能需要编写Runnable
EvenIterator
DataStructure
EvenIterator
DataStructureIterator itr = DataStructure.this.new EvenIterator();
这说明,我想在 的当前实例上创建,而不是当前实例EvenIterator
DataStructure
Runnable