理解this关键字的用法和作用,掌握当前对象引用、构造方法调用和方法链式调用
在Java中,this是一个特殊的关键字,它是对当前对象的引用。this关键字可以用来访问当前对象的成员变量、调用成员方法,以及调用其他构造方法。
当方法参数与成员变量同名时,使用this关键字可以明确区分它们:
public class Student {
private String name;
private int age;
// 使用this区分成员变量和参数
public void setName(String name) {
this.name = name; // this.name是成员变量,name是参数
}
public void setAge(int age) {
this.age = age; // this.age是成员变量,age是参数
}
// 不使用this的情况(参数名不同)
public void updateName(String newName) {
name = newName; // 没有歧义,可以不使用this
}
public void displayInfo() {
System.out.println("姓名: " + this.name); // 可以使用this,也可以省略
System.out.println("年龄: " + age); // 没有歧义时可以省略this
}
}
在一个构造方法中可以使用this()调用同一个类的其他构造方法,这样可以避免代码重复:
public class Person {
private String name;
private int age;
private String email;
// 默认构造方法
public Person() {
this("未知", 0); // 调用两参数的构造方法
}
// 两参数构造方法
public Person(String name, int age) {
this(name, age, null); // 调用三参数的构造方法
}
// 三参数构造方法(主构造方法)
public Person(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
System.out.println("创建Person对象: " + name);
}
// 错误示例:this()必须在第一行
/*
public Person(String name) {
System.out.println("开始创建对象"); // 错误!this()调用必须在第一行
this(name, 0);
}
*/
}
通过在方法中返回this,可以实现方法的链式调用,这种模式在构建器模式中非常常见:
public class StringBuilder2 {
private StringBuilder sb;
public StringBuilder2() {
this.sb = new StringBuilder();
}
// 返回this实现链式调用
public StringBuilder2 append(String str) {
sb.append(str);
return this; // 返回当前对象
}
public StringBuilder2 append(int num) {
sb.append(num);
return this; // 返回当前对象
}
public StringBuilder2 reverse() {
sb.reverse();
return this; // 返回当前对象
}
public String build() {
return sb.toString();
}
// 使用示例
public static void main(String[] args) {
StringBuilder2 builder = new StringBuilder2();
// 链式调用
String result = builder
.append("Hello")
.append(" ")
.append("World")
.append(123)
.reverse()
.build();
System.out.println(result); // 输出: 321dlroW olleH
}
}
下面是一个综合示例,展示了this关键字的各种用法:
public class BankAccount {
private String accountNumber;
private String ownerName;
private double balance;
// 构造方法重载
public BankAccount(String accountNumber) {
this(accountNumber, "未知用户", 0.0);
}
public BankAccount(String accountNumber, String ownerName) {
this(accountNumber, ownerName, 0.0);
}
public BankAccount(String accountNumber, String ownerName, double balance) {
this.accountNumber = accountNumber;
this.ownerName = ownerName;
this.balance = balance;
}
// 使用this区分成员变量和参数
public BankAccount setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
return this; // 支持链式调用
}
public BankAccount setOwnerName(String ownerName) {
this.ownerName = ownerName;
return this; // 支持链式调用
}
public BankAccount deposit(double amount) {
if (amount > 0) {
this.balance += amount;
System.out.println("存款成功,当前余额: " + this.balance);
}
return this; // 支持链式调用
}
public BankAccount withdraw(double amount) {
if (amount > 0 && amount <= this.balance) {
this.balance -= amount;
System.out.println("取款成功,当前余额: " + this.balance);
} else {
System.out.println("取款失败:余额不足或金额无效");
}
return this; // 支持链式调用
}
public void displayInfo() {
System.out.println("账户信息:");
System.out.println("账号: " + this.accountNumber);
System.out.println("户主: " + this.ownerName);
System.out.println("余额: " + this.balance);
}
}
// 1. 参数与成员变量同名时使用this
public void setName(String name) {
this.name = name;
}
// 2. 构造方法重载时使用this()
public Person(String name) {
this(name, 0);
}
// 3. 方法链式调用返回this
public Builder setValue(String value) {
this.value = value;
return this;
}
// 4. 提高代码可读性
public void process() {
this.validate();
this.execute();
}
// 1. this()不在构造方法第一行
public Person(String name) {
System.out.println("创建对象"); // 错误!
this(name, 0);
}
// 2. 构造方法循环调用
public Person() {
this("默认");
}
public Person(String name) {
this(); // 错误:形成循环调用
}
// 3. 过度使用this
public void method() {
this.field1 = this.field2; // 没必要都用this
}
概念 | 作用 | 使用场景 | 示例 |
---|---|---|---|
this | 引用当前对象 | 实例方法、构造方法 | this.name = name; |
super | 引用父类 | 继承关系中 | super.method(); |
static | 类级别的成员 | 不依赖对象实例 | static int count; |