在 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 = com.ThisAndThat.MyClass@74a14482
object reference = com.ThisAndThat.MyClass@74a14482在上面的示例中,我们创建了一个名为Main的对象obj。 然后,我们打印类的对象的引用obj和this。
在这里,我们可以看到obj和this的引用是相同的。 这意味着这不过是对当前对象的引用。
在许多情况下,通常都使用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);
}
}输出:
mc.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;
}
}this关键字的另一种常见用法是在类的获取器和设置器中。 例如:
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关键字:
- 在设置器方法中赋值
- 在获取器方法中访问值
在使用构造器重载的同时,我们可能不得不从另一个构造器调用一个构造器。 在这种情况下,我们不能显式调用构造器。 相反,我们必须使用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关键字将当前对象作为参数传递给方法。 例如,
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);在这里,我们通过将其传递为参数来调用add()方法。 由于this关键字包含对该类的对象obj的引用,因此我们可以在add()方法内更改x和y的值。