如何指从内部类封闭类? [英] How to refer to Enclosing class from Inner class?
本文介绍了如何指从内部类封闭类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的ArrayList扩展创建一个自定义的ArrayList一个可以使用正常的ArrayList方法在遍历它进行修改。对于这个我也创建一个Iterator。
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()方法,我需要确保该列表不会被修改(也可以在其他任何时间修改)。因此,我需要参考我的封闭类型在我同步()块。
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.
推荐答案
EnclosingType.this
。所以你的情况,这将是 SynchronizedList.this
。
EnclosingType.this
. So in your case, it would be SynchronizedList.this
.
这篇关于如何指从内部类封闭类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文