Java中的this关键字

121 阅读2分钟

this关键字

在这里插入图片描述 source


从图中我们可以看出Java中的this关键字主要有六种应用场景,下面我们通过代码来具体的理解一下其中几种最为常见的使用方法。

1. this调用当前属性

当使用类的构造方法对类的属性进行初始化时,使用this关键字有利于代码的阅读和理解。例如Student类如下所示:

class Student{
    String name;
    int age;
	
    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }
    public void showInfo(){
        System.out.println("Name is: " + name + " and age is: " + age);
    }
}

public class thisTest {
    public static void main(String[] args) {
    Student s = new Student("Forlogen", 18);
    s.showInfo(); // Name is: Forlogen and age is: 18
    }
}

如果不主动创建类的构建方法而使用默认的构造方法,当实例化Student的类对象后,我们就可以为类的属性就行赋值。而如果使用this关键字,同样调用构造方法进行初始化:

class Student{
    String name;
    int age;
	
    public Student(String name, int age){
        name = name;
        age = age;
    }
    public void showInfo(){
        System.out.println("Name is: " + name + " and age is: " + age);
    }
}

此时就会造成困惑,构造方法中的等号左右的name和age不容易一下明白哪一个是类属性,哪一个是参数。如下所示:

class Student{
    String name;
    int age;

    public Student(String name, int age){
        name = name;
        age = age;
    }
    public void showInfo(){
        System.out.println("Name is: " + name + " and age is: " + age);
    }
}

public class thisTest {
    public static void main(String[] args) {
    Student s = new Student("Forlogen", 18);
    s.showInfo(); // Name is: null and age is: 0
    }
}

2. this调用方法

  • 普通方法:虽然类内的方法可以不使用this关键字直接调用,但使用后可以增加代码的阅读性

    class Student{
        String name;
        int age;
    
        public Student(String name, int age){
            this.name = name;
            this.age = age;
            this.showInfo();
        }
        public void showInfo(){
            System.out.println("Name is: " + name + " and age is: " + age);
        }
    }
    
    public class thisTest {
        public static void main(String[] args) {
        Student s = new Student("Forlogen", 18);  // Name is: Forlogen and age is: 18
        }
    }
    
  • 构造方法:如上面所述,在类的构造方法内使用this关键字进行初始化属性变量时不易造成混淆。另外,如果此时构造方法有多个,如下所示:

    class Student{
        String name;
        int age;
        
    	public Student{
            
        }
        public Student(String name){
            this.name = name;
        }
        
        public Student(String name, int age){
            this.name = name;
            this.age = age;
        }
        public void showInfo(){
            System.out.println("Name is: " + name + " and age is: " + age);
        }
    }
    

    这样做虽然可以满足要求,类可以传入的参数不同调用不同的构造方法进行初始化,但有些冗余。更好的方法是使用this关键字进行构造方法的调用:

    class Student{
        String name;
        int age;
        
    	public Student{
            
        }
        public Student(String name){
            this();
            this.name = name;
        }
        
        public Student(String name, int age){
            this(name);
            this.age = age;
        }
        public void showInfo(){
            System.out.println("Name is: " + name + " and age is: " + age);
        }
    }
    

    NOTE:构造方法中调用this必须将其放在方法体的第一行!

  • this表示当前方法通过谁调用的方法,谁就是this。如下所示,在showInfo()this.name是通过main()中的Student对象s调用的,那么this就是s

    package com.company;
    
    class Student{
        String name;
        int age;
    
        public void showInfo(String name){
            System.out.println(name + " love" + this.name);
            System.out.println(this);
        }
    }
    
    public class thisTest {
        public static void main(String[] args) {
            Student s = new Student();
            s.name = "Kobe";
            s.showInfo("Forlogen"); 
            // Forlogen loveKobe
    	    //com.company.Student@1b6d3586
        }
    }
    

java中this关键字的作用