gpt4 book ai didi

Java:this关键字前面有类名

转载 作者:行者123 更新时间:2023-12-03 21:09:30 25 4
gpt4 key购买 nike

我在 jdk 8 的 ArrayList.java 中找到了一个片段:

    @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; 我觉得很奇怪。

我认为ArrayList.this 等同于这里的this。我对吗?如果有区别,使用 ArrayList.this 而不是 this 有什么好处?

最佳答案

If there's a difference, what's the benefit of using ArrayList.this over this

一个内部类引用了一个外部类。要使用外部类 this,您需要将外部类的类放在它之前。

注意:在这种情况下,this 是一个迭代器,并且没有名为 elementData 的字段

关于Java:this关键字前面有类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24947750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com