如何从Inner类引用Enclosing类?

use*_*784 3 java nested class arraylist synchronized

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

public class SynchronizedList<E> extends ArrayList<E>
{
    // Fields here

    //Constructors and methods here

public class SynchronizedListIterator<E> implements Iterator<E>
{
    public int index;
    private E current;

    public boolean hasNext()
    {
        synchronized (/* reference to enclosing List object */) {
                    //code goes here
        }
        return false;
    }

    //more methods here
}
}
Run Code Online (Sandbox Code Playgroud)

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

Jef*_*rey 5

EnclosingType.this.所以在你的情况下,它会SynchronizedList.this.