This keyword in Java (three usages)

Three uses of this keyword:

     1. Accessing a member variable through the this keyword can solve the problem of local variable name conflicts.
Examples:

public class Student {
    
    
   String name;//成员变量name
   int age;//成员变量age
   long phone;//成员变量phone
   String address;//成员变量address
    public Student(String name, int age, long phone, String address) {
    
    
        this.name = name;
        this.age = age;
        this.phone = phone;
        this.address = address;
    }
}

The parameters name, age, etc. defined in the construction method in the above code are member variables, and member variables such as name and age are also defined in the class. The use of age in the construction method is to access local variables. If this.age is used, it is Access member variables.
     2. Invoke member methods through this keyword
Examples:

public class Student {
    
    
    public void A(){
    
    
        System.out.println("A方法执行了......");
    }
    public void B(){
    
    
        this.A();//调用A()方法
    }
}

In the B() method, use this.A() to access the A() method. Note that the this keyword here can be omitted, and the effect is the same.

public class Student {
    
    
    public void A(){
    
    
        System.out.println("A方法执行了......");
    }
    public void B(){
    
    
        A();//调用A()方法
    }
}

     3. Access the construction method this([parameter 1, parameter 2,...])
instance in the construction method :

public class Student {
    
     
    String name;//成员变量name
    public Student() {
    
    
        this("张三");
    }

    public Student(String name) {
    
    
        this.name = name;//访问有参构造方法
    }

    public void print(){
    
    
        System.out.println("姓名:" + name);
    }

    public static void main(String[] args) {
    
    
        Student student = new Student();
        student.print();
    }
}

In the non-parameter construction method, the parameter construction method is called, and the following points should be noted when using this to call the construction method:

  • You can only use this() in the constructor to call other constructors, not in member methods.
  • In the constructor, the statement that uses this() to call the constructor must be on the first line and can only appear once.
  • You cannot use this() to call each other in two constructors in a class.
public class Student {
    
    
    String name;//成员变量name
    public Student() {
    
    
        System.out.println("无参构造方法...");
        this("张三");//会错误,必须位于第一行
    }
    public Student(String name) {
    
    
        this.name = name;//访问有参构造方法
    }
  }
public class Student {
    
    
    String name;//成员变量name
    public Student() {
    
    
        this("张三");//错误,不能相互调用
    }
    public Student(String name) {
    
    
        this();//错误,不能相互调用
        this.name = name;//访问有参构造方法
    }
}

Come on! ! !

Guess you like

Origin blog.csdn.net/qq_42494654/article/details/109190433