[英]How to refer to Enclosing class from Inner class?
I am extending ArrayList to create a custom ArrayList that can be modified using normal ArrayList methods while iterating over it.我正在扩展 ArrayList 以创建一个自定义的 ArrayList,可以在迭代它时使用普通的 ArrayList 方法对其进行修改。 For this I am also creating an 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
}
}
During my hasNext() and next() methods, I need to make sure the list is not modified (it can be modified at any other time).在 hasNext() 和 next() 方法期间,我需要确保列表未被修改(它可以在任何其他时间修改)。 Hence I need to refer to my enclosing type in my synchronized() block.
因此,我需要在我的 synchronized() 块中引用我的封闭类型。
EnclosingType.this
. EnclosingType.this
。 So in your case, it would be SynchronizedList.this
.所以在你的情况下,它将是
SynchronizedList.this
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.