java - 这段代码中的 "this"到底指的是什么?

标签 java linked-list this

public CharList(CharList l) 
{
    // Whatever method your CharList provides to get the 
    // first node in the list goes here
    CharNode pt = l.head(); 

    // create a new head node for *this* list
    CharNode newNode = new CharNode();
    this.head = newNode;

    // Go through old list, copy data, create new nodes 
    // for this list.
    while(pt != null)
    {
        newNode.setCharacter(pt.getCharacter());
        pt = pt.getNext();
        if (pt != null)
        {
            newNode.setNext(new CharNode());
            newNode = newNode.getNext();
        }

    }
} 

我认为 this 用于引用对象 A,如“A.addElement(car);”,但在这种情况下,我不知道 this 指的是什么......而且我没有看到这样做的要点: this.head = newNode;因为 this.head 再也没有被使用过。

最佳答案

this 指的是 CharList 的当前实例,this.head 指的是实例字段 head 。如果没有同名的局部变量,您可以丢弃 this 关键字来访问实例字段。

关于java - 这段代码中的 "this"到底指的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15723716/

相关文章:

java - 如何在所有子项目中调用Gradle任务?

java - 我的 jar 文件运行但没有任何结果

c++ - 赋值运算符链表c++

c++ - 无法弄清楚如何使用 C++ 中的链表按从 a 到 z 的顺序显示字母表

javascript - 如何在 JavaScript 上访问 "this"的父级?

c++ - 如何从 C++ 内部类引用封闭实例?

java - 核心java打印蝴蝶结构

java - Tomcat7 - org.apache.catalina.LifecycleException : Failed to start component

python - (Python) 单链表 - Leetcode

java - 用 this 调用实例方法