如何从Inner类引用Enclosing类?

我正在扩展ArrayList以创建一个自定义ArrayList,可以在迭代它时使用常规ArrayList方法进行修改。 为此,我也创建了一个迭代器。

public class SynchronizedList extends ArrayList { // Fields here //Constructors and methods here public class SynchronizedListIterator implements Iterator { public int index; private E current; public boolean hasNext() { synchronized (/* reference to enclosing List object */) { //code goes here } return false; } //more methods here } } 

在我的hasNext()和next()方法期间,我需要确保列表未被修改(可以在任何其他时间修改它)。 因此,我需要在synchronized()块中引用我的封闭类型。

EnclosingType.this 。 所以在你的情况下,它将是SynchronizedList.this