I know a lot of friends are like me: In the Java program seems to often see "this", they occasionally use it, but in the end "This" how to use, but'm not sure! Many people mention it, say "current object", but in the end what is the current object, what is the current object, he himself is not clear. Now let's look at a small example to share with you the use of "this" in Java!
Copy Code code as follows:
/**
* @author Fengzhi-neusoft
*
* This example is to illustrate the three uses of this!
*/
Package test;
public class Thistest {
private int i=0;
First constructor: There is an int type parameter
thistest (int i) {
this.i=i+1;//Now this represents the reference member variable I, not the function argument I
System.out.println ("Int constructor i--this.i: +i+"-"+this.i");
System.out.println ("I-1:" + (i-1) + "this.i+1:" + (this.i+1));
The results from both outputs amply prove that I and this.i are not the same!
}
Second constructor: has a string parameter
Thistest (String s) {
System.out.println ("String constructor:" +s);
}
Third constructor: has an int shape parameter and a string parameter
Thistest (int i,string s) {
This (s);//this calls the second constructor
This (i);
/* This is not available because no other method can call the constructor, only the constructor can call him.
It must be noted, however, that the constructor call constructor must also be the first row, and the construction method can only tune
Use one and only one time builder! */
This.i=i++;//this to reference the member variables of the class
System.out.println ("Int constructor:" +i+ "/n" + "String constructor:" +s);
}
Public Thistest increment () {
this.i++;
Return this;//Returns the current object, which belongs to (Thistest)
}
public static void Main (string[] args) {
Thistest tt0=new thistest (10);
Thistest tt1=new thistest ("OK");
Thistest tt2=new thistest ("OK again!");
System.out.println (Tt0.increment (). Increment (). Increment (). i);
Tt0.increment () returns a Thistest object that is i++ on tt0 basis.
Then return to the I++ object on the basis of the object returned above Thistest!
}
}
Run Result:
Int Constructor i--this.i:10--11
String Constructor:ok
String Constructor:ok again!
Int constructor:21
String Constructor:ok again!
14
The details of the note has been written more clearly, here is not to repeat, but summed up, in fact, this is mainly to three kinds of usage:
1. Represents a reference to the current object!
2, representing the class of the member variable, rather than function parameters, note that the function parameters and member variables have the same name is to distinguish! In fact, this is the first use of the special case, more commonly used, so come out to emphasize.
3. Used to refer to constructors in the constructor method that satisfy the specified parameter type (in fact, the constructor method). But here it must be very important to note that only one construction method can be referenced and must be at the beginning!
There is also the note: This cannot be used in the static method! So even the definition of a static method is that there's no way to do it! Although exaggerated, it is fully stated that this cannot be used in the static method!