Haven't figured out the this keyword in Java? Just read this article.

In class today, sister Ran Ran took me to ask: Little brother, little brother, I heard that you are learning Java, do you know this keyword?

me? This? (It's over. I learned nothing when I was studying. Now I was asked about it. I was still a sister. Wouldn't it be embarrassing if I couldn't answer).

Ran Ran sister paper: Do you know exactly? No, no, I heard that you have learned Java for a long time, and you don’t even know this keyword!

Me: Yes, it's not easy, wait for me to tell you slowly (giao, seize the opportunity, add points, this is, you have to say something)

Okay, come on, tell you this keyword

The role of this keyword:

1. Distinguish between member variables and local variables (when member variables and local variables have the same name)

2. You can call other construction methods of the same class in the construction method (but it is stipulated that this can only be written in the first line of the construction method)

3. It can call the construction method, and of course it can also call the ordinary method (in the method in the same class, it is not necessary to use this to call, just write the method name directly, but some people think that write this It will make the code look more standardized. I think this is completely unnecessary. Let’s look at personal habits here.)~

Me: You see, isn't it simple?

Ran Ran: What, that's it? I know, are you saying something useless to me?

Me: Don't be angry, then I will tell you a little bit (thinking about it)

4. This keyword can also be returned as a reference to the current object. I will show you the code and you will understand

public class Student {
    public Student log2(){
        System.out.println("直接返回一个Student的引用");
        return this;
    }
}

Just write. You see, how simple

Ran Ran: I didn't understand, what object or reference was returned. . .

Me: Well. . . . Okay, then I will explain to you. Look at the code again!

public class TestMain {
    public static void main(String[] args) {
        Student student = new Student();
        Student student2 = student.log2();
        System.out.println(student == student2);
    }
}

You see, you new object (object reference), and then call log2 this method, it will return another object, this time this object is the same as your new one. But if you directly return new Student();   then this object is a new object, so this can return the current object (reference) . got it!

Ran Ran: So, then you say so, I know.

Me: I'll give you another break, don't worry;

Do you know that if there are two Student objects, they both have to call the same method. How can you know which object is calling this method? Such as this code

public class Student {
    public void log2(int i) {
        System.out.println(i); 
    }
}
public class TestMain {
    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();
        s1.log2(2);
        s2.log2(3);
    }
}

Do you think this is a reference to two different objects? Of course, you can distinguish who is calling it!

In fact, the essence is not like this

After you write the code, the compiler is quietly helping you to do a job, in order to make you worry, how intimate, if I am...

Ran Ran: You mean, what did you do?

Me: Okay; I said. . .

When you call the method, the compiler actually adds the current object before the parameters you pass, like this

s1.log2(s1,2);
s2.log2(s2,3);

Of course, if you write the code like this, the compiler will definitely say: I'll do this for you for this little thing, so I won't bother the little master, and then ruthlessly give you a red wave;

Ran Ran: It's true, it's so mysterious.

Me: Harm, you still don’t believe me, look at the code below, let’s make a small change in the method

public class Student {
    public void log2(int i) {
        System.out.println(this + " " + i);
    }
}
public class TestMain {
    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();
        System.out.println("s1的地址----->" + s1);
        System.out.println("s2的地址----->" + s2);
        s1.log2(2);
        s2.log2(3);
    }
}

In order to see if this is the case, let's print out the address pointed to by s1 and s2 in the main method, and find that it is the same as the address printed by this in the method

This shows that the object is indeed passed in.

Ran Ran: Ah, I understand, I understand, it turned out to be like this, it's amazing. me...

Me: What's wrong with you, is there anything else you don't understand?

Ran Ran: I can't remember who I am referring to when distinguishing variables...

Me: This, ah, simple, you remember the principle of "this." Who is called by this this management is the member variable. If your variable has the same name, if you don't point to it, your local variable is used by default.

Teacher: What are you two muttering? I’ve been muttering for half a class, what fun things are there to share, let’s all have fun together!

to sum up:

Because I personally feel that if it is just a boring stacking concept and only tells a few terms, it will not only look boring, but will also be completely forgotten after reading it. So the notes displayed in this way may be more memorable. There is no fixed method for learning, no matter how you learn, just learn it!

If there is an error, please point it out! Correct in time

Guess you like

Origin blog.csdn.net/weixin_44231805/article/details/109184166