Java:这个关键字前面有类名 [英] Java: this keyword preceded by class name

查看:16
本文介绍了Java:这个关键字前面有类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 比 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆