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/