Java this 关键字
在 Java 中,this 关键字用于引用当前对象。在本文中,我们将通过示例了解 Java 中的 this 关键字,如何以及在何处使用 this。
在本文中,我们将通过示例了解 Java 中的 this
关键字,如何以及在何处使用 this
。
this 关键字
在 Java 中, this
关键字用于在方法或构造方法中引用当前对象。例如:
public class Main {
int instVar;
Main(int instVar){
this.instVar = instVar;
System.out.println("this\t= " + this);
}
public static void main(String[] args) {
Main obj = new Main(8);
System.out.println("object\t= " + obj);
}
}
输出:
this = [email protected]
object = [email protected]
在上面的例子中,我们创建了一个名为 obj
对象,然后我们打印 obj
对象。
根据输出,我们可以看到 obj
对象和 this
的值时一样的。这意味着 this
是对当前对象的引用。
使用 this 关键字
this
常用关键字有多种情况。
将 this 用于歧义变量
在 Java 中,不允许在一个作用域(类作用域或方法作用域)内声明两个或多个同名变量。但是,实例变量和参数可能具有相同的名称。例如,
public class MyClass {
// 实例变量
int age;
// 参数
MyClass(int age){
age = age;
}
}
在上面的程序中,实例变量和参数同名:age
,Java 编译器由于名称歧义而感到困惑。
在这种情况下,我们要使用 this 关键字解决这个问题。
首先,让我们看一个不使用 this
关键字的例子:
public 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
关键字重写上面的代码。
public 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.age
明确了这是调用的对象的实例变量,因此对象的 age
变量被赋值为 8.
此外,如果参数和实例变量的名称不同,编译器会自动附加 this 关键字。如下代码:
public class Main {
int age;
Main(int i) {
age = i;
}
}
相当于:
public class Main {
int age;
Main(int i) {
this.age = i;
}
}
this 与 Getter, Setter
this
关键字还经常用于类的 setter 和 getter 方法中。例如:
public class Main {
String name;
// setter
void setName( String name ) {
this.name = name;
}
// getter
String getName(){
return this.name;
}
public static void main( String[] args ) {
Main obj = new Main();
obj.setName("Toshiba");
System.out.println("obj.name: "+obj.getName());
}
}
输出:
obj.name: Toshiba
在构造方法重载中使用它
在使用构造方法重载时,我们可能必须从一个构造方法调用另一个构造方法。在这种情况下,我们不能显式调用构造方法,我们必须使用 this
关键字。
在这里,我们使用了 this
关键字的另一种形式。也就是说, this()
。我们举个例子:
public class Complex {
private int a, b;
// 2 个参数的构造方法
private Complex( int i, int j ){
this.a = i;
this.b = j;
}
// 1 个参数的构造方法
private Complex(int i){
// 调用 2 个参数的构造方法
this(i, i);
}
// 没有参数的构造方法
private Complex(){
// 调用 1 个参数的构造方法
this(0);
}
@Override
public String toString(){
return this.a + " + " + this.b + "i";
}
public static void main( String[] args ) {
// 使用 2 个参数创建对象
Complex c1 = new Complex(2, 3);
// 使用 1 个参数创建对象
Complex c2 = new Complex(3);
// 不使用参数创建对象
Complex c3 = new Complex();
// 打印对象
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()
的方法,因此会输出我们自己的 toString()
方法的返回值。
this()
的巨大优势之一是减少了重复代码的数量。
! 注意:从一个构造方法调用另一个构造方法称为显式构造方法调用。
将 this 作为参数传递
我们可以使用 this
关键字将当前对象作为参数传递给方法。例如,
class ThisExample {
// 实例变量
int x;
int y;
ThisExample(int x, int y) {
// 在构造方法中为实例变量赋值
this.x = x;
this.y = y;
// 调用 add() 方法之前的 x 和 y 的值
System.out.println("Before passing this to add() method:");
System.out.println("x = " + this.x + ", y = " + this.y);
// 使用 this 作为参数调用 add() 方法
add(this);
// 调用 add() 方法之后的 x 和 y 的值
System.out.println("After passing this to add() method:");
System.out.println("x = " + this.x + ", y = " + this.y);
}
void add(ThisExample o){
o.x += 2;
o.y += 2;
}
}
public class Main {
public static void main( String[] args ) {
ThisExample obj = new ThisExample(1, -2);
}
}
输出:
Before passing this to add() method:
x = 1, y = -2
After passing this to add() method:
x = 3, y = 0
在上面的例子中,在构造方法中 ThisExample()
,注意这一行:
add(this);
在这里,我们将 this
作为参数传递来调用 add()
方法。由于 this
关键字包含对 obj
对象的引用,我们可以在 add()
方法里面改变 x
和 y
的值。