Skip to content

10.5 this 关键字

this 关键字的概念

this 是 Java 中的一个关键字,它表示当前对象的引用。this 关键字在类的方法和构造方法中使用,用于引用当前对象的属性和方法。

this 关键字的用法

1. 引用当前对象的属性

当方法或构造方法中的参数名与类的属性名相同时,可以使用 this 关键字来区分参数和属性。

示例:

java
public class Person {
    private String name;
    private int age;
    
    // 构造方法
    public Person(String name, int age) {
        this.name = name; // 使用 this 引用当前对象的属性
        this.age = age;   // 使用 this 引用当前对象的属性
    }
    
    // setter 方法
    public void setName(String name) {
        this.name = name; // 使用 this 引用当前对象的属性
    }
    
    public void setAge(int age) {
        this.age = age; // 使用 this 引用当前对象的属性
    }
}

2. 调用当前对象的方法

可以使用 this 关键字调用当前对象的其他方法。

示例:

java
public class Person {
    private String name;
    private int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        this.printInfo(); // 使用 this 调用当前对象的方法
    }
    
    public void printInfo() {
        System.out.println("Name: " + this.name); // 使用 this 引用当前对象的属性
        System.out.println("Age: " + this.age);   // 使用 this 引用当前对象的属性
    }
    
    public void updateInfo(String name, int age) {
        this.name = name;
        this.age = age;
        this.printInfo(); // 使用 this 调用当前对象的方法
    }
}

3. 调用当前类的其他构造方法

在构造方法中,可以使用 this() 调用当前类的其他构造方法。

示例:

java
public class Person {
    private String name;
    private int age;
    private String address;
    
    // 无参构造方法
    public Person() {
        this("Unknown", 0, "Unknown"); // 调用有参构造方法
    }
    
    // 有一个参数的构造方法
    public Person(String name) {
        this(name, 0, "Unknown"); // 调用有参构造方法
    }
    
    // 有三个参数的构造方法
    public Person(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }
}

4. 返回当前对象

在方法中,可以使用 this 关键字返回当前对象,实现方法链式调用。

示例:

java
public class Person {
    private String name;
    private int age;
    
    public Person setName(String name) {
        this.name = name;
        return this; // 返回当前对象
    }
    
    public Person setAge(int age) {
        this.age = age;
        return this; // 返回当前对象
    }
    
    public void printInfo() {
        System.out.println("Name: " + this.name);
        System.out.println("Age: " + this.age);
    }
}

// 使用方法链式调用
Person person = new Person();
person.setName("John").setAge(25).printInfo();

this 关键字的使用场景

1. 解决变量名冲突

当方法或构造方法中的参数名与类的属性名相同时,使用 this 关键字来区分参数和属性。

示例:

java
public class Student {
    private String name;
    private int studentId;
    
    // 构造方法
    public Student(String name, int studentId) {
        this.name = name; // 区分参数 name 和属性 name
        this.studentId = studentId; // 区分参数 studentId 和属性 studentId
    }
}

2. 调用其他构造方法

在构造方法中,使用 this() 调用其他构造方法,减少代码重复。

示例:

java
public class Car {
    private String brand;
    private String model;
    private int year;
    
    // 无参构造方法
    public Car() {
        this("Unknown", "Unknown", 0); // 调用有参构造方法
    }
    
    // 有参构造方法
    public Car(String brand, String model, int year) {
        this.brand = brand;
        this.model = model;
        this.year = year;
    }
}

3. 实现方法链式调用

在方法中返回 this,实现方法链式调用,使代码更加简洁。

示例:

java
public class Calculator {
    private int result;
    
    public Calculator add(int number) {
        this.result += number;
        return this;
    }
    
    public Calculator subtract(int number) {
        this.result -= number;
        return this;
    }
    
    public Calculator multiply(int number) {
        this.result *= number;
        return this;
    }
    
    public Calculator divide(int number) {
        this.result /= number;
        return this;
    }
    
    public int getResult() {
        return this.result;
    }
}

// 使用方法链式调用
Calculator calculator = new Calculator();
int result = calculator.add(10).subtract(5).multiply(2).divide(2).getResult();
System.out.println("Result: " + result); // 输出 5

4. 作为参数传递

可以将 this 作为参数传递给其他方法,引用当前对象。

示例:

java
public class Person {
    private String name;
    private int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public void register() {
        Database.registerPerson(this); // 将当前对象作为参数传递
    }
}

public class Database {
    public static void registerPerson(Person person) {
        // 注册人员信息
        System.out.println("Registering person: " + person.getName());
    }
}

示例:this 关键字的综合使用

示例 1:基本使用

java
public class Person {
    private String name;
    private int age;
    
    // 构造方法
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    // setter 方法
    public Person setName(String name) {
        this.name = name;
        return this;
    }
    
    public Person setAge(int age) {
        this.age = age;
        return this;
    }
    
    // getter 方法
    public String getName() {
        return this.name;
    }
    
    public int getAge() {
        return this.age;
    }
    
    // 其他方法
    public void printInfo() {
        System.out.println("Name: " + this.name);
        System.out.println("Age: " + this.age);
    }
}

public class PersonExample {
    public static void main(String[] args) {
        // 创建对象
        Person person = new Person("John", 25);
        person.printInfo();
        
        // 使用方法链式调用
        person.setName("Jane").setAge(30).printInfo();
    }
}

示例 2:构造方法调用

java
public class Student {
    private String name;
    private int studentId;
    private double grade;
    
    // 无参构造方法
    public Student() {
        this("Unknown", 0, 0.0);
        System.out.println("调用无参构造方法");
    }
    
    // 有一个参数的构造方法
    public Student(String name) {
        this(name, 0, 0.0);
        System.out.println("调用有一个参数的构造方法");
    }
    
    // 有三个参数的构造方法
    public Student(String name, int studentId, double grade) {
        this.name = name;
        this.studentId = studentId;
        this.grade = grade;
        System.out.println("调用有三个参数的构造方法");
    }
    
    // 方法
    public void printInfo() {
        System.out.println("Name: " + this.name);
        System.out.println("Student ID: " + this.studentId);
        System.out.println("Grade: " + this.grade);
    }
}

public class StudentExample {
    public static void main(String[] args) {
        System.out.println("创建 student1:");
        Student student1 = new Student();
        student1.printInfo();
        
        System.out.println("\n创建 student2:");
        Student student2 = new Student("John");
        student2.printInfo();
        
        System.out.println("\n创建 student3:");
        Student student3 = new Student("Jane", 1001, 85.5);
        student3.printInfo();
    }
}

示例 3:方法链式调用

java
public class Car {
    private String brand;
    private String model;
    private int year;
    private String color;
    
    // 构造方法
    public Car() {
    }
    
    // setter 方法
    public Car setBrand(String brand) {
        this.brand = brand;
        return this;
    }
    
    public Car setModel(String model) {
        this.model = model;
        return this;
    }
    
    public Car setYear(int year) {
        this.year = year;
        return this;
    }
    
    public Car setColor(String color) {
        this.color = color;
        return this;
    }
    
    // 方法
    public void printInfo() {
        System.out.println("Brand: " + this.brand);
        System.out.println("Model: " + this.model);
        System.out.println("Year: " + this.year);
        System.out.println("Color: " + this.color);
    }
}

public class CarExample {
    public static void main(String[] args) {
        // 使用方法链式调用
        Car car = new Car()
            .setBrand("Toyota")
            .setModel("Camry")
            .setYear(2020)
            .setColor("Red");
        car.printInfo();
    }
}

this 关键字的注意事项

  1. this 只能在实例方法和构造方法中使用this 关键字不能在静态方法中使用,因为静态方法属于类,不属于对象。

  2. this 不能在类的外部使用this 关键字只能在类的内部使用,不能在类的外部使用。

  3. this() 必须是构造方法的第一条语句:在构造方法中调用其他构造方法时,this() 必须是第一条语句。

  4. this 不能用于引用静态属性和静态方法this 表示当前对象,而静态属性和静态方法属于类,不属于对象。

常见问题

1. 在静态方法中使用 this

症状:编译错误:Cannot use this in a static context

解决方案this 关键字不能在静态方法中使用,因为静态方法属于类,不属于对象。

示例:

java
public class Person {
    private static String name;
    
    // 错误:在静态方法中使用 this
    public static void setName(String name) {
        this.name = name; // 编译错误
    }
}

2. this() 不是构造方法的第一条语句

症状:编译错误:Constructor call must be the first statement in a constructor

解决方案:在构造方法中调用其他构造方法时,this() 必须是第一条语句。

示例:

java
public class Person {
    private String name;
    private int age;
    
    public Person() {
        System.out.println("Hello");
        this("Unknown", 0); // 错误:this() 必须是第一条语句
    }
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

3. 变量名冲突

症状:逻辑错误,参数值没有正确赋值给属性

解决方案:使用 this 关键字区分参数和属性。

示例:

java
public class Person {
    private String name;
    
    // 错误:变量名冲突,参数 name 覆盖了属性 name
    public void setName(String name) {
        name = name; // 错误:参数 name 赋值给自身
    }
    
    // 正确:使用 this 区分参数和属性
    public void setNameCorrect(String name) {
        this.name = name; // 正确:参数 name 赋值给属性 name
    }
}

最佳实践

  1. 使用 this 区分参数和属性:当方法或构造方法中的参数名与类的属性名相同时,使用 this 关键字来区分。

  2. 使用 this() 调用其他构造方法:在构造方法中,使用 this() 调用其他构造方法,减少代码重复。

  3. 使用 this 实现方法链式调用:在方法中返回 this,实现方法链式调用,使代码更加简洁。

  4. 使用 this 作为参数传递:将 this 作为参数传递给其他方法,引用当前对象。

  5. 避免在静态方法中使用 thisthis 关键字不能在静态方法中使用,因为静态方法属于类,不属于对象。

总结

this 关键字是 Java 中的一个重要关键字,它表示当前对象的引用。this 关键字的主要用途包括:

  1. 引用当前对象的属性:当方法或构造方法中的参数名与类的属性名相同时,使用 this 关键字来区分。

  2. 调用当前对象的方法:使用 this 关键字调用当前对象的其他方法。

  3. 调用当前类的其他构造方法:在构造方法中,使用 this() 调用当前类的其他构造方法。

  4. 返回当前对象:在方法中返回 this,实现方法链式调用。

  5. 作为参数传递:将 this 作为参数传递给其他方法,引用当前对象。

通过合理使用 this 关键字,可以使代码更加清晰、简洁,提高代码的可读性和可维护性。

© 2026 编程马·菜鸟教程 版权所有