Java this 关键字
Java this 关键字
在本文中,我们将通过示例了解 Java 中的这个关键字,以及如何以及在何处使用它们。
这个关键字
在 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 类的 .然后我们打印对对象 obj 的引用 和 this
类的关键字。
在这里,我们可以看到 obj 的引用 和 this
是一样的。这意味着这只不过是对当前对象的引用。
使用此关键字
this
有多种情况 关键字是常用的。
将其用于歧义变量名称
在 Java 中,不允许在一个作用域(类作用域或方法作用域)内声明两个或多个具有相同名称的变量。但是,实例变量和参数可能具有相同的名称。例如,
class MyClass {
// instance variable
int age;
// parameter
MyClass(int age){
age = age;
}
}
在上面的程序中,实例变量和参数同名:age。在这里,Java 编译器由于名称歧义而感到困惑。
在这种情况下,我们使用这个关键字。例如,
首先我们来看一个不使用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 .
此外,如果参数和实例变量的名称不同,编译器会自动附加此关键字。例如代码:
class Main {
int age;
Main(int i) {
age = i;
}
}
相当于:
class Main {
int age;
Main(int i) {
this.age = i;
}
}
这与 Getter 和 Setter
this
的另一个常见用法 关键字在 setters 和 getters 方法中 一类的。例如:
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 方法中访问值
在构造函数重载中使用 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, int j)
从构造函数Complex(int i)
- 调用构造函数
Complex(int i)
来自构造函数Complex()
注意这一行,
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 的引用 类,我们可以改变 x 的值 和 y add()
内 方法。
java