Java:此关键字前面有类名 [英] Java: this keyword preceded by class name
问题描述
我从jdk 8的ArrayList.java中找到一个代码段:
I find a snippet in ArrayList.java from jdk 8:
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
这行:Object[] elementData = ArrayList.this.elementData;
在我看来很奇怪.
The line: Object[] elementData = ArrayList.this.elementData;
looks strange to me.
我认为ArrayList.this
等效于this
.我对吗?如果存在差异,使用ArrayList.this
而不是this
有什么好处?
I think ArrayList.this
is equivalent to this
here. Am I right? If there's a difference, what's the benefit of using ArrayList.this
over this
?
推荐答案
如果有区别,那么使用ArrayList.this有什么好处
If there's a difference, what's the benefit of using ArrayList.this over this
内部类对外部类的引用.要使用外部类this
,请将外部类的类放在它前面.
An Inner class has a reference to an outer class. To use the outer class this
you put the class of the outer class before it.
注意:在这种情况下,this
是一个迭代器,并且没有名为elementData
Note: In this case this
is an Iterator and doesn't have a field called elementData
这篇关于Java:此关键字前面有类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!