本文介绍了如何从内部类引用封闭类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在扩展 ArrayList 以创建一个自定义 ArrayList,可以在对其进行迭代时使用普通的 ArrayList 方法对其进行修改.为此,我还创建了一个迭代器.
I am extending ArrayList to create a custom ArrayList that can be modified using normal ArrayList methods while iterating over it. 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
}
}
在我的 hasNext() 和 next() 方法中,我需要确保列表没有被修改(可以在任何其他时间修改).因此我需要在我的 synchronized() 块中引用我的封闭类型.
During my hasNext() and next() methods, I need to make sure the list is not modified (it can be modified at any other time). Hence I need to refer to my enclosing type in my synchronized() block.
推荐答案
EnclosureType.this
.所以在你的情况下,它将是 SynchronizedList.this
.
EnclosingType.this
. So in your case, it would be SynchronizedList.this
.
这篇关于如何从内部类引用封闭类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!