this in Java
this keyword is equivalent to "I" in Putonghua.
Xiao Ming said, "I ate." At this time, "I" stands for Xiao Ming.
Xiao Hong said, "I ate." At this time, "I" stands for Xiao Hong.
"I" represents the current person
Step 1: This represents the current object
public class Hero { String name; //Full name float hp; //Blood volume float armor; //Armor int moveSpeed; //Moving speed //Print virtual addresses in memory public void showAddressInMemory(){ System.out.println("Printing this See the virtual address:"+this); } public static void main(String[] args) { Hero garen = new Hero(); garen.name = "Galen"; //Print the object directly, displaying the virtual address of the object in memory //Format: Hero@c17164 c17164 is a virtual address, each execution, the address is not necessarily the same. System.out.println("The virtual address that the print object sees:"+garen); //Call showAddressInMemory, print this of the object, and display the same virtual address garen.showAddressInMemory(); Hero teemo = new Hero(); teemo.name = "Ti Mo"; System.out.println("The virtual address that the print object sees:"+teemo); teemo.showAddressInMemory(); } }
Step 2: Access properties through this
Accessing the properties of an object through this keyword
public class Hero { String name; //Full name float hp; //Blood volume float armor; //Armor int moveSpeed; //Moving speed //The parameter name is the same as the attribute name. //In the method body, only the parameter name can be accessed public void setName1(String name){ name = name; } //To avoid setName1 problems, parameter names have to use other variable names public void setName2(String heroName){ name = heroName; } //Accessing properties through this public void setName3(String name){ //Name represents the parameter name //this.name represents the attribute name this.name = name; } public static void main(String[] args) { Hero h =new Hero(); h.setName1("teemo"); System.out.println(h.name); h.setName2("garen"); System.out.println(h.name); h.setName3("Death song"); System.out.println(h.name); } }
Step 3: Call other construction methods through this
If you want to call another constructor in one constructor, you can use this()
public class Hero { String name; //Full name float hp; //Blood volume float armor; //Armor int moveSpeed; //Moving speed //Constructing Method with One Parameter public Hero(String name){ System.out.println("A Method of Constructing a Parameter"); this.name = name; } //Construction Method with Two Parameters public Hero(String name,float hp){ this(name); System.out.println("Construction Method of Two Parameters"); this.hp = hp; } public static void main(String[] args) { Hero teemo = new Hero("Ti Mo",383); System.out.println(teemo.name); } }
Practice: Construction method (this)
(Design a construction method with parameters of
String name float hp float armor int moveSpeed
Moreover, in this constructor, the constructor is called
public Hero(String name,float hp))
Answer:
public class Hero { String name; // Full name float hp; // Blood volume float armor; // Armor int moveSpeed; // Moving speed // Constructing Method with One Parameter public Hero(String name) { System.out.println("A Method of Constructing a Parameter"); this.name = name; } // Construction Method with Two Parameters public Hero(String name, float hp) { this(name); System.out.println("Construction Method of Two Parameters"); this.hp = hp; } // Construction Method with Four Parameters public Hero(String name, float hp, float armor, int moveSpeed) { this(name,hp); this.armor = armor; this.moveSpeed = moveSpeed; } public static void main(String[] args) { Hero teemo = new Hero("Ti Mo", 383); System.out.println(teemo.name); Hero db = new Hero("Dead brother",400,27,360); System.out.println(db.moveSpeed); } }