------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
First look at a small program:
1 class Person2 { 3 PrivateString name; 4 Private intAge ; 5Person (intAge//when the local variable is age, the member variable is also6 { 7 This. Age = Age;//This can be very well differentiated8 } 9 Person (String name)Ten { One This. name = name;//This is used here to represent the object that called the constructor method A } -Person (String name,intAge ) - { the This. Name =name; - This. Age =Age ; - } - + Public voidspeak () - { +System.out.println ("name=" +name+ "... age=" +Age ); A Show (); at } - Public voidShow () - { -System.out.println ( This. Name); - } -}
The above procedure synthesizes the constructor and the use of this. It looks like a case where the local variable has the same name as the member variable.
This represents the reference to the object to which the function belongs.
Simply put: Which object is the object that represents the function that this is located in.
The application of this:
When defining a function in a class, the function is used internally to represent the object when the function is called.
Whenever this type of function is used within this class of objects, it is represented by this.
This statement:
Used to call each other between constructors. such as: this (name);
The THI statement can only be defined in the first row of the constructor. Because initialization is performed first.
A summary of this:
This is used in two ways: 1, to distinguish between variables of the same name, said member and local name of the time, 2, for the constructor between the call.
Note: The general function cannot call the constructor directly, because the this statement cannot be used in a generic function and can only be used between constructors.
Dark Horse programmer--this Keywords