/**
* This exists in the method and is called in the method.
* and is called in a non-static method. (This represents the current instance of this class, and the static method does not depend on any instance of the class, and is loaded as the class is produced, so this cannot be referenced within the method.) )
* For more information: Java static usage and principle http://www.cnblogs.com/itcqx/p/5519464.html
* Object Call method, this pointer points to which object calls the method then the pointer to the object's reference is assigned to This,this.
* This example is to illustrate the three usage of this!
*/
Package test;
Public class thistest {
Private int i=0;
First constructor: has an int type parameter
Thistest (int i) {
this. i=i+1;//the this represents the reference member variable I, not the function parameter 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 the two outputs amply demonstrate that I and this.i are not the same!
}
Second constructor: There is a string shape parameter
Thistest (String s) {
System. out. println ("String constructor:" +s);
}
Third constructor: There is an int type parameter and a string shape parameter
Thistest (int i,string s) {
This (s);//this call second constructor
This (i);
/* cannot be used here, because no other method can call the constructor, only the constructor will call him.
However, it is important to note that the constructor is called by the construction method, and it must be the first line, and the construction method can only be adjusted
Use one and only once constructors! */
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 that 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 a tt0 basis,
It then returns the Thistest object on the basis of the object returned above i++!
}
}
Operation Result:
Int Constructor i--this.i:10--11
String Constructor:ok
String Constructor:ok again!
Int constructor:21
String Constructor:ok again!
14
(1) When a non-static method parameter is passed, there is an implicit parameter of this, which is the object itself that invokes the method.
For example, Object O=new object ();
O.tostring (); is actually a parameter-passed ToString (Object this), and O==this
This can be used in non-static methods to get the other fields and methods of the calling object, as well as the private domain.
(2) A static method belongs to a class and does not belong to an object . So there is no implicit parameter this, and nature cannot invoke the object itself through this.
This does not mean that you cannot call a non-static domain. We can do this by displaying parameter passing:
class
MyClass{
private
int
nonStaticData=
0
;
private
static
int
staticData=
0
;
public
static
void
operStaticMethod(MyClass mc){
System.out.println(staticData);
//直接使用类中的静态域
System.out.println(mc.nonStaticData);
//使用对象mc中的非静态域
}
}
MyClass c=
new
MyClass();
MyClass.operStaticMethod(c);
Details of the comments have been written more clearly, here is not to repeat, just summed up, in fact, this is the main three ways to use:
1. Represents a reference to the current object!
2, the use of class member variables, rather than function parameters, note that the function parameters and member variables with the same name are differentiated! In fact, this is the first use of the special case, more commonly used, so that come out to emphasize.
3, used in the construction method to reference the constructor that satisfies the specified parameter type (in fact, the constructor method). But there must be a lot of attention here: only one construction method can be referenced and must be at the beginning!
There is also note: This cannot be used in the static method! So even the definition of a static method is: There is no method for this! Although exaggerated, it is fully explained that this cannot be used in the static method! (because static is generated as the class is generated, and this represents the current class object, it needs to be instantiated before it is generated, so it cannot be called again in Static).
The usage and principle of Java this