Three functions of this keyword
- Attribute in current class: this. Attribute name
- Methods in the current class: this() --- paparazzi method, this. Method name () --- ordinary method
- Refers to the current object
this call property
Our previous construction method is defined as follows
public Person(String n, int a) { name = n; age = a; }
Parameters n and a are assigned to the future name and age attributes respectively. But such encroachment is easy to make people don't know the meaning. So the best way is as follows
public Person(String name, int age) { name = name; age = age; }
However, such a definition construction method will bring a problem: the value cannot be assigned successfully during instantiation
public class Demo { public static void main(String[] args) { new Person("Anonymous object", 66).showInfo(); } }
This is due to the variable scope of Java. Name and age are already defined in the constructor. Referring to the proximity principle, the name and age used in the construction are the name and age in the parameters. Instead of the attribute name, age. To solve this problem, we can use the name of this. Attribute to specify the attribute
class Person { private String name; // Person's attribute name private int age; // Human attribute age // Define the construction method. The method name is consistent with the class name and has no return value public Person(String name, int age) { this.name = name; this.age = age; } public void showInfo() { System.out.println("name: " + this.name + "\nage: " + this.age); } // The setter and getter methods are omitted } public class Demo { public static void main(String[] args) { new Person("Anonymous object", 66).showInfo(); } }
Of course, you can also use the previous definition construction method. But that method is not recommended. In future programs, the specification is that as long as it is an attribute, it needs to use the this keyword to access it
this calls the method
It is all right to call common methods in class without adding this keyword. But the standard way of writing still needs to add this
Construction method
The constructor is called when the object is instantiated. So this() is only allowed on the first line of the constructor. Since this is used in the construction method, it is possible to form an endless loop, which needs special attention when using
class Person { private String name; // Person's attribute name private int age; // Human attribute age // Define the construction method. The method name is consistent with the class name and has no return value public Person(String name, int age) { this(name); this.age = age; } // Define a parameter structure public Person(String name) { this(); this.name = name; } // Define parameterless construction public Person() { System.out.println("Parameterless construct called"); } public void uselessFunc() { System.out.println("A useless method call"); } public void showInfo() { System.out.println("name: " + this.name + "\nage: " + this.age); } // The setter and getter methods are omitted } public class Demo { public static void main(String[] args) { new Person("Anonymous object", 66).showInfo(); } }
Common method
public void uselessFunc() { System.out.println("A useless method call"); } public void showInfo() { this.uselessFunc(); System.out.println("name: " + this.name + "\nage: " + this.age); } public class Demo { public static void main(String[] args) { new Person("Anonymous object", 66).showInfo(); } }
Comprehensive example
Implement a student class that contains four attributes (name, age, sex, stuNo). Contains four construction methods
Nonparametric Construction: default setting name: stu1, age: 18, sex: male, stuNo:1000
One parameter structure: pass in name. Default setting: age: 20, sex: female, stuNo:111111
Two parameter structure: pass in name and stuNo. Default setting: 20
Three meal structure: pass in name, stuNo, age
Four parameter structure: pass in name, stuNo, age, sex
A method to return student information getInfo()
Method 1: do not call the construction method with this()
class Student { // Define properties private String name; // full name private long stuNo; // Student number private int age; // Age private String sex; // Gender // Define parameterless construction public Student() { this.name = "stu1"; this.stuNo = 10000L; this.age = 18; this.sex = "male"; } // One parameter structure public Student(String name) { this.name = name; this.stuNo = 111111L; this.age= 20; this.sex = "female"; } // Two parameter structure public Student(String name, long stuNo) { this.name = name; this.stuNo = stuNo; this.age = 20; } //Three parameter structure public Student(String name, long stuNo, int age) { this.name = name; this.stuNo = stuNo; this.age = age; } // Four parameter structure public Student(String name, long stuNo, int age, String sex) { this.name = name; this.stuNo = stuNo; this.age = age; this.sex = sex; } // Return information public String getInfo() { return "full name:" + this.name + "\n Student number: " + this.stuNo + "\n Age:" + this.age + "\n Gender:" + this.sex; } } public class Demo { public static void main(String[] args) { Student stu = new Student("stu", 188888L, 22, "male"); System.out.println(stu.getInfo()); } }
You can see that there is a lot of duplicate code in the constructor above, so you can use this() for optimization
class Student { // Define properties private String name; // full name private long stuNo; // Student number private int age; // Age private String sex; // Gender // Define parameterless construction public Student() { this("stu1", 10000L, 18, "male"); } // One parameter structure public Student(String name) { this(name, 111111L, 20, "female"); } // Two parameter structure public Student(String name, long stuNo) { this(name, stuNo, 20, null); // The default value of String is null } //Three parameter structure public Student(String name, long stuNo, int age) { this(name, stuNo, age, null); } // Four parameter structure public Student(String name, long stuNo, int age, String sex) { this.name = name; this.stuNo = stuNo; this.age = age; this.sex = sex; } // Return information public String getInfo() { return "full name:" + this.name + "\n Student number: " + this.stuNo + "\n Age:" + this.age + "\n Gender:" + this.sex; } } public class Demo { public static void main(String[] args) { Student stu = new Student("stuThis", 188888L, 22, "male"); System.out.println(stu.getInfo()); } }
Using this() flexibly in development can greatly simplify the code.