简体   繁体   English

这段代码中“ this”到底指的是什么?

[英]What does “this” refer to exactly in this code?

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();
        }

    }
} 

I thought that this is used to refer to the Object A as in "A.addElement(car);", but in this case I don't know what this refers to... And I don't see the point in doing: this.head = newNode; 我以为这是在“ A.addElement(car);”中用来指代对象A的,但是在这种情况下,我不知道它指的是什么...而且我看不出这样做的意义:this.head = newNode; since this.head is never used again. 因为this.head不再使用。

this refers to the current instance of CharList , and this.head refers to the instance field head . this指向CharList的当前实例, this.head指向实例字段head You can discard this keyword to access instance fields if there are no local variables with the same name. 如果没有同名的本地变量,则可以放弃this关键字来访问实例字段。

The docs explain what this is: 文档解释了是什么:

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. 在实例方法或构造函数中,这是对当前对象的引用-当前对象正在调用其方法或构造函数。 You can refer to any member of the current object from within an instance method or a constructor by using this. 您可以使用此方法从实例方法或构造函数中引用当前对象的任何成员。

The keyword this refers to the current instance of CharList . 关键字this指向CharList的当前实例。 It is useful for referring to variables that may share the same at class level, otherwise it can be omitted. 对于引用可能在类级别共享相同变量的变量很有用,否则可以省略。

Here, no local variable head does not appear in the constructor of CharList , so can be written as: 在这里, CharList的构造函数中没有局部变量head ,所以可以这样写:

head = newNode;

this.head is never used again. this.head不再使用。

Since head is a member variable of the class, the value set in the constructor will be used in other methods of the class. 由于head是该类的成员变量,因此在构造函数中设置的值将在该类的其他方法中使用。

Possible duplicate of What is the meaning of "this" in Java? Java中“ this”的含义的可能重复项 , but anyway: , 但无论如何:

It's a reference to the specific instance of the object you're working with. 它是对您正在使用的对象的特定实例的引用。 So, if I have (going to write this in C#, sorry): 因此,如果我有(对不起,请使用C#编写):

public class MyObject
{
    public MyObject(string AString) 
    {
         MyString = AString;
    }

    private string MyString;

    public string WhatsMyStringCalled()
    {
         return this.MyString;
    }
}

If I were to construct an instance of MyObject, I would expect WhatsMyStringCalled to return the MyString property associated with that particular instance. 如果要构造MyObject的实例,我希望WhatsMyStringCalled返回与该特定实例关联的MyString属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM