Use of this Keyword in Java Foundation
- 2021-10-13 07:50:40
- OfStack
1. The role of the this keyword
The this keyword not only emphasizes the methods in this class, but also has the following functions.
1. Representing properties in a class
2. You can call constructors in this class using keywords
3. this represents the current object
1.1 Representing attributes in a class
package thiss;
class Person{
private int age;
private String name;
public Person(int age,String name){
this.age=age;// Is the age Attribute assignment
this.name=name;// Is the name Attribute assignment
}
public String getinfo(){
return " Name is :"+name+","+" Age is: "+age;
}
}
public class ThisDemo {
public static void main(String[] args) {
Person person = new Person(15," Zhang 3");
person.getinfo();
System.out.println(person.getinfo());
}
}
1.2 Calling constructors using this
If you have more than one constructor in one class, you can also call the constructor using this. The constructor is invoked using this (), which must be placed on line 1 of the constructor.
package thiss;
class Person1{
private int age;
private String name;
public Person1(){// Parametric construction method
}
public Person1(String name){//1 Construction method of three parameters
this();// Calling a parameterless constructor
this.name=name;
}
public Person1(int age,String name){// Construction method with two parameters
this(name);// Calls with 1 Construction method of three parameters
this.age=age;
}
public String getinfo(){
return " Name is :"+name+","+" Age is: "+age;
}
}
public class ThisDemo01 {
public static void main(String[] args) {
Person1 person = new Person1 (15," Zhang 3");// Call the constructor to instantiate the object and initialize the member properties.
person.getinfo();
System.out.println(person.getinfo());// Call the method to output the age and name.
}
}
When a constructor is invoked using this (), at least one constructor is invoked without this ().
Recursive invocation of constructor: recursive constructor invocation//Avoid recursive invocation, as shown in the following code
package thiss;
class Person1{
private int age;
private String name;
public Person1(){// Parametric construction method
**this(" Li 4",10);// This will lead to recursive calls, and compilation will not pass **
}
public Person1(String name){//1 Construction method of three parameters
this();// Calling a parameterless constructor
this.name=name;
}
public Person1(int age,String name){// Construction method with two parameters
this(name);// Calls with 1 Construction method of three parameters
this.age=age;
}
public String getinfo(){
return " Name is :"+name+","+" Age is: "+age;
}
}
public class ThisDemo01 {
public static void main(String[] args) {
Person1 person = new Person1 (15," Zhang 3");// Call the constructor to instantiate the object and initialize the member properties.
person.getinfo();
System.out.println(person.getinfo());// Call the method to output the age and name.
}
}
1.3 You can use this to represent the current object
package thiss;
class Person2 {
public String getinfo() {
System.out.println("Person Objects of the class --- " " + this);
return null;
}
}
public class ThisDemo02 {
public static void main(String[] args) {
Person2 person = new Person2();// Call the constructor to instantiate the object
System.out.println(" Output main Objects in the --- " " + person);// Output person Objects in the
person.getinfo();// Call Person2 Is the object in the main Objects in the same
}
}
E:\ Java\ jdk\ bin\ java. exe "-javaagent: E:\ Java\ IDEA\ IntelliJ 2019.1.3\ lib\ idea_rt. jar=58205: E:\ Java\ IDEA\ ES60IDEA 2019.1.3\ bin"-Dfile. encoding = UTF-8-classpath E:\ Java\ E:\ Java\ jdk\ jre\ lib\ deploy.jar; E:\ Java\ jdk\ jre\ lib\ ext\ access-bridge-64. jar; E:\ Java\ jdk\ jre\ lib\ ext\ cldrdata.jar; E:\ Java\ jdk\ jre\ lib\ ext\ dnsns. jar; E:\ Java\ jdk\ jre\ lib\ ext\ jaccess.jar; E:\ Java\ jdk\ jre\ lib\ ext\ jfxrt. jar; E:\ Java\ jdk\ jre\ lib\ ext\ localedata. jar; E:\ Java\ jdk\ jre\ lib\ ext\ nashorn.jar; E:\ Java\ jdk\ jre\ lib\ ext\ sunec. jar; E:\ Java\ jdk\ jre\ lib\ ext\ sunjce_provider. jar; E:\ Java\ jdk\ jre\ lib\ ext\ sunmscapi. jar; E:\ Java\ jdk\ jre\ lib\ ext\ sunpkcs11.jar; E:\ Java\ jdk\ jre\ lib\ ext\ zipfs. jar; E:\ Java\ jdk\ jre\ lib\ javaws. jar; E:\ Java\ jdk\ jre\ lib\ jce. jar; E:\ Java\ jdk\ jre\ lib\ jfr.jar; E:\ Java\ jdk\ jre\ lib\ jfxswt. jar; E:\ Java\ jdk\ jre\ lib\ jsse.jar; E:\ Java\ jdk\ jre\ lib\ management-agent. jar; E:\ Java\ jdk\ jre\ lib\ plugin. jar; E:\ Java\ jdk\ jre\ lib\ resources. jar; E:\ Java\ jdk\ jre\ lib\ rt.jar; E:\ Java\ project\ out\ production\ Demo01 thiss.ThisDemo02
Output objects in main-"thiss. Person2 @ 4554617c
Objects of the Person class--"thiss. Person2 @ 4554617c
Process finished with exit code 0
1.4 Compare whether two objects are identical
class Person{ // Definition Person Class
private String name ; // Name
private int age ; // Age
public Person(String name,int age){
this.setName(name) ;
this.setAge(age) ;
}
public boolean compare(Person per){
// When this method is called, there are two objects inside: the current object and the passed-in object
Person p1 = this ; // The current object represents the per1
Person p2 = per ; // The object passed in represents the per2
if(p1==p2){ // Judge whether it is the same 1 Objects, compared by address
return true ;
}
// After that, it is judged that each 1 Are the attributes equal
if(p1.name.equals(p2.name)&&p1.age==p2.age){
return true ; // Two objects are equal
}else{
return false ; // Two objects are not equal
}
}
public void setName(String name){ // Set the name
this.name = name ;
}
public void setAge(int age){ // Set age
this.age = age ;
}
public String getName(){
return this.name ;
}
public int getAge(){
return this.age ;
}
};
public class ThisDemo08{
public static void main(String args[]){
Person per1 = new Person(" Zhang 3",30) ; // Declare two objects with exactly the same contents
Person per2 = new Person(" Zhang 3",30) ; // Declare two objects with exactly the same contents
// Get each attribute directly in the main method in turn for comparison
if(per1.compare(per2)){
System.out.println(" Two objects are equal! ") ;
}else{
System.out.println(" Two objects are not equal! ") ;
}
}
};