This article mainly introduces relevant information about the detailed examples of This usage in Java. I hope that through this article everyone can understand and master the use of this keyword. Friends in need can refer to it
Java Detailed explanation of examples of This usage in
When defining a variable with a class name, what is defined is only a reference, and the outside world can access the properties and methods in this class through this reference.
Is there enough or should there be a reference in the class to access its own properties and methods?
Haha, Java provides a very good thing, which is the this object, which can reference the properties and methods of this class in the class. Let’s take a simple example first:
public class ThisDemo { String name="Mick"; public void print(String name){ System.out.println("类中的属性 name="+this.name); System.out.println("局部传参的属性="+name); } public static void main(String[] args) { ThisDemo tt=new ThisDemo(); tt.print("Orson"); } }
Regarding returning a reference to the class itself, there is a classic example in "Thinking in Java".
Return the object itself through the this keyword and then implement multiple operations in one statement, or post it.
public class ThisDemo { int number; ThisDemo increment(){ number++; return this; } private void print(){ System.out.println("number="+number); } public static void main(String[] args) { ThisDemo tt=new ThisDemo(); tt.increment().increment().increment().print(); } }
Define two constructors in a class, and call the other constructor through this reference in one constructor. This should be possible.
What is the use of such an implementation mechanism in actual application development? Post the code written below:
public class ThisDemo { String name; int age; public ThisDemo (){ this.age=21; } public ThisDemo(String name,int age){ this(); this.name="Mick"; } private void print(){ System.out.println("最终名字="+this.name); System.out.println("最终的年龄="+this.age); } public static void main(String[] args) { ThisDemo tt=new ThisDemo("",0); //随便传进去的参数 tt.print(); } }
Although the above code is very short, it seems to be very logical to understand. Name is assigned in the constructor with parameters and without parameters. Assign the age attribute to it.
But I personally think there is a problem. When instantiating a class, you should first allocate memory for the object ThisDemo and call the constructor ThisDemo(String name, int age) first.
When executing the first line, call the ThisDemo() constructor, which means there should be two memory spaces, one is the memory space allocated for ThisDemo(String name, int age) and the other Is the execution space of ThisDemo().
Why are the two attributes name and age in the same object instantiated in the final printed result? Please enlighten me!
To summarize:
1) The this keyword is a reference to itself inside the class, which can facilitate methods in the class to access its own properties;
2) You can return a reference to the object's own class, and you can also call another constructor in a constructor.
The above is the detailed content of Detailed explanation of how to use This in Java. For more information, please follow other related articles on the PHP Chinese website!