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

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

问题描述

我从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屋!

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