this 关键字
this 是一个引用,指当前对象——也就是正在执行这个方法的对象。
为什么要用 this
看这个例子:
java
public class Person {
private String name;
public Person(String name) {
// 这里有两个「name」,哪个是哪个?
name = name; // 这行代码什么都没做!
}
}这行 name = name; 没有把参数赋给字段,因为左右两边的 name 都是局部变量参数。正确的写法是:
java
public class Person(String name) {
// this.name 明确表示「当前对象的 name」
this.name = name;
}this 的几种用法
1. 区分成员变量和局部变量
这是 this 最常见的用途:
java
public class User {
private String name; // 成员变量
private int age; // 成员变量
public void setName(String name) { // name 是局部变量(参数)
this.name = name; // this.name = 成员变量 = 参数
}
}2. 在构造方法中调用其他构造方法
用 this() 调用同类中的其他构造方法:
java
public class Person {
private String name;
private int age;
private String gender;
private String address;
public Person() {
// 调用双参构造
this("未知", 0);
}
public Person(String name) {
// 调用双参构造
this(name, 0);
}
public Person(String name, int age) {
// 调用三参构造
this(name, age, "未知");
}
public Person(String name, int age, String gender) {
// 调用全参构造
this(name, age, gender, "未知");
}
// 全参构造:真正做初始化的地方
public Person(String name, int age, String gender, String address) {
this.name = name;
this.age = age;
this.gender = gender;
this.address = address;
}
}这样所有构造方法最终都汇聚到全参构造,初始化逻辑只写一次。
3. 返回当前对象(链式调用)
用 return this 返回当前对象,可以实现链式调用:
java
public class StringBuilder {
private String value = "";
public StringBuilder append(String str) {
this.value += str;
return this; // 返回当前对象
}
public StringBuilder append(int num) {
this.value += num;
return this; // 返回当前对象
}
public String build() {
return this.value;
}
}
// 链式调用
String result = new StringBuilder()
.append("Hello")
.append(" ")
.append("World")
.build();这在 StringBuilder、StringBuffer 以及 Builder 模式中非常常见。
4. 作为参数传递
把当前对象传递给其他方法:
java
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void introduce() {
System.out.println("我叫 " + this.name);
}
public void invite(Person other) {
System.out.println(this.name + " 邀请 " + other.name + " 参加派对");
}
}this 不能用在 static 方法中
static 方法属于类,不属于对象,所以没有 this:
java
public class Counter {
private static int count = 0;
public static void increment() {
// ❌ 错误:static 方法中没有 this
// this.count++;
// ✅ 正确:直接访问静态成员
count++;
}
}this 与 super 对比
| 对比项 | this | super |
|---|---|---|
| 指向 | 当前对象 | 父类对象 |
| 调用属性 | 当前类的属性(可能遮蔽) | 父类的属性 |
| 调用方法 | 当前类的方法(可能遮蔽) | 父类的方法 |
| 调用构造 | 当前类的其他构造方法 | 父类的构造方法 |
| static 方法中 | ❌ 不能使用 | ❌ 不能使用 |
java
class Parent {
String name = "Parent";
void print() {
System.out.println(this.name);
}
}
class Child extends Parent {
String name = "Child";
void print() {
System.out.println(this.name); // Child(当前对象的)
System.out.println(super.name); // Parent(父类的)
this.print(); // 调用当前类的方法
super.print(); // 调用父类的方法
}
}常见误区
误区 1:以为 this 可以访问私有成员
this 访问成员的权限和直接访问是一样的:
java
public class Outer {
private int value;
public class Inner {
private int value;
void access() {
this.value = 10; // 访问的是 Inner 的 value
// 不能直接访问 Outer's value
// Outer.this.value = 10; // 这是另一种语法
}
}
}误区 2:所有地方都要写 this
不是的,只有在歧义时才需要:
java
public class User {
private String name;
// ✅ 不需要 this:没有歧义
public String getName() {
return name; // 没有其他 name 变量
}
// ✅ 需要 this:参数遮蔽了成员变量
public void setName(String name) {
this.name = name;
}
}总结
this = 当前对象 = 正在执行方法的那个对象| 场景 | 示例 |
|---|---|
| 区分成员/局部变量 | this.name = name |
| 构造方法链 | this(...) |
| 返回当前对象 | return this |
| 传递自身 | invite(this) |
记住:this 只在实例方法和构造方法中存在,静态方法里没有 this。
