如何从内部类中引用封闭类?

3

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

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
}
}

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

可能重复的问题:访问外部类的实例获取内部类对象的外部类对象 - Paul Bellora
1个回答

5

EnclosingType.this。因此,在您的情况下,它将是SynchronizedList.this


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接