First, this keyword overview:
The This keyword in Java can do the following:
1. Calling properties in a class
2. Calling methods or constructing methods in a class
3. Represents the current object
Second, this represents the current object
Represents the reference to the object to which the function belongs, which is simply the object that is calling the function where this is located.
Public classThisdemo { Public Static voidMain (string[] args) {person P=NewPerson (); //p.setname ("Libushi"); //System.out.println (P.getname ());Person p1=NewPerson ("Haungjianfeng"); Person P2=NewPerson ("Haungjianfeng", 21); }}classperson{Private intAge ; PrivateString name; Person () {//System.out.println ("A:name=" +name+ ", age=" +age);} person (String name) { This(); This. name =name;//which object calls the function, this represents which object//System.out.println ("B:name=" +name+ ", age=" +age);} person (String name,intAge ) { //this.name =name; //This can be used to take the above constructor function . This(name);//This is called the This statement, which is used to call between constructors the this statement can only be defined in the first row of the constructor, because the initialization action is performed first This. age=Age ; //System.out.println ("C:name=" +name+ ", age=" +age); } Public voidsetName (String name) { This. Name =name; } PublicString GetName () {returnname; } Public voidspeak () {System.out.println ("Name=" +name+ ",, age=" +Age ); //System.out.println ("Name=" +this.name+ ", age=" +this.age);//which object calls him, this represents which object's name and age } Public voidcry () {System.out.println ("Cry ..."); }}
Public classninetyfive { Public Static voidMain (string[] args) {Bear B1=NewBear ("Small white", ' public ');//The object is created here, and when the object is initialized, the corresponding constructor of the class is called, then this represents the B1 object .Bear B2 =NewBear (); }}classbear{PrivateString name; Private Charsex; PublicBear () { This("Big Bear");//call the following constructor, which must be the first line in the method (called down between constructors) } PublicBear (String name) { This(Name, ' Public ');//Call the following construction method (called down between constructors) } PublicBear (String name,Charsex) { This. name = name;//invokes a property in the class that represents the name of the current object, which object calls this to indicate which object's name This. Sex =sex; This. Bite ();//calling methods in this class } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public CharGetsex () {returnsex; } Public voidSetsex (Charsex) { This. Sex =sex; } //the way to bite people Public voidBite () {System.out.println ("I Am" +sex+ "bear--" +name); } }
third, the application of this:
When a function is to be defined in a class, it is used inside the function to represent the object when the function is called.
1. Requirements: Define a function to compare the age of the same
1 Public classThistest {2 Public Static voidMain (string[] args) {3person P1 =NewPerson (20);4person P2 =NewPerson (25);5 Booleanb =P1.compare (p2);6 System.out.println (b);7 }8 }9 classperson{Ten Private intAge ; OnePerson (intAge ) { A This. Age =Age ; - } - Public BooleanCompare (person p) { the //You can use the IF Else statement, or you can use the ternary operator - return This. age==p.age;//This can be omitted, plus reading is better - } -}
Object-oriented _this keywords