The 1.this keyword represents the current object
This . Property Operation Properties of the current object
This method invokes the current object.
2. The This keyword is often used when encapsulating the properties of an object
Public classTelphone {Private floatScreen ; Private floatCPU; Private floatMem; Public voidSendMessage () {System.out.println ("SendMessage"); } Public floatGetscreen () {returnScreen ; } Public voidSetscreen (floatScreen ) { This. screen = screen;//manipulate the properties of the current object This. SendMessage ();//method that invokes the current object } Public floatgetcpu () {returnCPU; } Public voidSetCpu (floatCPU) { This. CPU =CPU; } Public floatGetmem () {returnMem; } Public voidSetmem (floatmem) { This. Mem =Mem; } PublicTelphone () {System.out.println ("Com.imooc.Telphone"); } PublicTelphone (floatNewscreen,floatNEWCPU,floatNewmem) { if(newscreen<3.5f) {System.out.println ("..............."); screen= 3.5f; } CPU=newcpu; Mem=Newmem; System.out.println ("----------------"); }}This
This is an object of its own, representing the object itself, which can be understood as: a pointer to the object itself .
The usage of this can be broadly divided into 3 types in Java:
1. Normal Direct references
This does not have to be said, this is equivalent to pointing to the current object itself.
2. The name of the member who participates in the membership is distinguished by this:
PackageS2;classPerson {Private intAge = 22; PublicPerson () {System.out.println ("Age of Initialization:" +Age ); } Public intGetage (intAge ) { This. Age = Age;//The name of the member in the form return This. Age; }} Public classTest03 { Public Static voidMain (string[] args) {person Harry=NewPerson (); System.err.println ("Harry's age is:" + harry.getage (33)); }}
Operation Result:
Age of initialization:Harry' s aged is:33
3. Reference constructors
Let's talk about this with super, see below
The This keyword in Java