在 Java 中,this 关键字用于引用方法或构造函数中的当前对象。例如:
class Main {
int instVar;
Main(int instVar){
this.instVar = instVar;
System.out.println("this reference = " + this);
}
public static void main(String[] args) {
Main obj = new Main(8);
System.out.println("object reference = " + obj);
}
}
输出:
this reference = Main@23fc625e object reference = Main@23fc625e
在上面的示例中,我们创建了一个名为 obj 的 Main 类的对象。然后我们打印类 Main 的对象 obj 和 this
关键字的引用。
在这里,我们可以看到 obj 和 this
的引用是相同的。这意味着 this 只是当前对象的引用。
this 关键字的用法
this
关键字在许多情况下被常用。
1. 使用 this 来区分具有相同名称的变量
在 Java 中,不允许在作用域(类作用域或方法作用域)中声明两个或多个同名变量。但是,实例变量和参数可能具有相同的名称。例如:
class MyClass {
// instance variable
int age;
// parameter
MyClass(int age){
age = age;
}
}
在上面的程序中,实例变量和参数的名称相同:age。在这里,Java 编译器由于名称歧义而感到困惑。
在这种情况下,我们使用 this 关键字。例如:
首先,让我们看一个不使用 this
关键字的示例
class Main {
int age;
Main(int age){
age = age;
}
public static void main(String[] args) {
Main obj = new Main(8);
System.out.println("obj.age = " + obj.age);
}
}
输出:
obj.age = 0
在上面的示例中,我们将 8
作为值传递给构造函数。但是,我们得到 0
的输出。这是因为 Java 编译器由于实例变量和参数之间的名称歧义而感到困惑。
现在,让我们使用 this
关键字重写上面的代码。
class Main {
int age;
Main(int age){
this.age = age;
}
public static void main(String[] args) {
Main obj = new Main(8);
System.out.println("obj.age = " + obj.age);
}
}
输出:
obj.age = 8
现在,我们得到了预期的输出。这是因为当调用构造函数时,构造函数中的 this
被调用构造函数的对象 obj 替换。因此,age 变量被赋值为 8。
另外,如果参数名和实例变量名不同,编译器会自动添加 this 关键字。例如,代码
class Main {
int age;
Main(int i) {
age = i;
}
}
等同于
class Main {
int age;
Main(int i) {
this.age = i;
}
}
2. 构造函数中的 this
this
关键字的另一个常见用法是类的setter 和 getter 方法。例如:
class Main {
String name;
// setter method
void setName( String name ) {
this.name = name;
}
// getter method
String getName(){
return this.name;
}
public static void main( String[] args ) {
Main obj = new Main();
// calling the setter and the getter method
obj.setName("Toshiba");
System.out.println("obj.name: "+obj.getName());
}
}
输出:
obj.name: Toshiba
在这里,我们使用了 this
关键字
- 在 setter 方法中赋值
- 在 getter 方法中访问值
3. 在构造函数重载中使用 this
在处理构造函数重载时,我们可能需要从一个构造函数调用另一个构造函数。在这种情况下,我们不能显式调用构造函数。相反,我们必须使用 this
关键字。
在这里,我们使用 this 关键字的不同形式。即 this()
。让我们看一个例子:
class Complex {
private int a, b;
// constructor with 2 parameters
private Complex( int i, int j ){
this.a = i;
this.b = j;
}
// constructor with single parameter
private Complex(int i){
// invokes the constructor with 2 parameters
this(i, i);
}
// constructor with no parameter
private Complex(){
// invokes the constructor with single parameter
this(0);
}
@Override
public String toString(){
return this.a + " + " + this.b + "i";
}
public static void main( String[] args ) {
// creating object of Complex class
// calls the constructor with 2 parameters
Complex c1 = new Complex(2, 3);
// calls the constructor with a single parameter
Complex c2 = new Complex(3);
// calls the constructor with no parameters
Complex c3 = new Complex();
// print objects
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}
}
输出:
2 + 3i 3 + 3i 0 + 0i
在上面的示例中,我们使用了 this
关键字,
- 从构造函数
Complex(int i)
调用构造函数Complex(int i, int j)
- 从构造函数
Complex()
调用构造函数Complex(int i)
注意这行:
System.out.println(c1);
在这里,当我们打印对象 c1 时,对象被转换为一个字符串。在此过程中,会调用 toString()。由于我们在类中覆盖了 toString()
方法,因此我们根据该方法获得输出。
this()
的一个巨大优点是减少了重复代码量。但是,在使用 this()
时应始终小心。
这是因为从一个构造函数调用另一个构造函数会增加开销,这是一个缓慢的过程。使用 this()
的另一个巨大优点是减少了重复代码量。
注意:从一个构造函数调用另一个构造函数称为显式构造函数调用。
将 this 作为参数传递
我们可以使用 this
关键字将当前对象作为参数传递给方法。例如:
class ThisExample {
// declare variables
int x;
int y;
ThisExample(int x, int y) {
// assign values of variables inside constructor
this.x = x;
this.y = y;
// value of x and y before calling add()
System.out.println("Before passing this to addTwo() method:");
System.out.println("x = " + this.x + ", y = " + this.y);
// call the add() method passing this as argument
add(this);
// value of x and y after calling add()
System.out.println("After passing this to addTwo() method:");
System.out.println("x = " + this.x + ", y = " + this.y);
}
void add(ThisExample o){
o.x += 2;
o.y += 2;
}
}
class Main {
public static void main( String[] args ) {
ThisExample obj = new ThisExample(1, -2);
}
}
输出:
Before passing this to addTwo() method: x = 1, y = -2 After passing this to addTwo() method: x = 3, y = 0
在上面的示例中,在构造函数 ThisExample()
中,注意这行:
add(this);
这里,我们通过传递 this 作为参数来调用 add()
方法。由于 this 关键字包含类对象 obj 的引用,因此我们可以在 add()
方法中更改 x 和 y 的值。