Java this关键字
Java中的" this"关键字是什么?
Java这个关键字返回对当前Object的引用。
我们可以访问对象变量,使用此关键字调用当前对象的方法或者构造函数。
Java的" this"关键字可用于从实例方法或者构造函数中引用当前对象的任何成员。
此关键字主要用于避免类属性和参数之间的混淆。
例如,如果成员函数的参数名称与对象变量的名称相同,则该功能可能无法正常工作。
Java this关键字示例
假设我们有一个像下面这样的程序。
public class Item{
String name;
//Constructor with a parameter and this keyword
public Item(String name) {
this.name = name;
}
//Call the constructor
public static void main(String[] args) {
Item Obj = new Item("car");
System.out.println(Obj.name);
}
}
输出" this"作为对当前对象的引用
Java此关键字示例
代码将项目名称打印为" car"。
现在,让我们看看从构造函数中删除" this"关键字时会发生什么。
public class Item{
String name;
//Constructor with a parameter and without this keyword
public Item(String name) {
name = name;
}
//Call the constructor
public static void main(String[] args) {
Item Obj = new Item("car");
System.out.println(Obj.name);
}
}
输出不带" this"作为对当前对象的引用
输出不带" this"关键字
上面的示例显示了此变量在访问对象属性中的应用。
我们无法创建两个具有相同名称的局部变量。
但是,允许创建一个名称相同的实例变量和一个局部变量或者方法参数。
在这种情况下,局部变量将隐藏实例变量,这称为变量阴影。
为解决此问题,我们将此关键字与字段一起使用以指向实例变量而不是局部变量。
" this"关键字的常规用法
Java的" this"关键字可用于引用当前的类实例变量。
我们可以使用this()来调用当前的类构造函数。
我们也可以在this()语句中传递参数。Java的" this"关键字可用于(隐式)调用当前的类方法。
关键字" this"可以在方法调用中作为参数传递。
我们可以使用" this"关键字来返回当前的类实例。
如果出现可变阴影,我们可以使用" this"关键字访问对象属性。
还有一种特殊类型的构造函数,称为复制构造函数。
Java没有默认的副本构造函数,但是我们可以使用此关键字显式创建它。
复制构造函数和display()方法的" this"关键字
package com.theitroad.examples;
public class Blog {
String name;
int popularity;
//parameterized constructor
public Blog(String name, int popularity) {
this.name = name;
this.popularity = popularity;
}
//Copy-Constructor
public Blog(Blog b) {
this.popularity = b.popularity;
this.name = b.name;
}
public void display() {
System.out.println("name: " + this.name);
System.out.println("popularity: " + this.popularity + " %");
}
public static void main(String[] args) {
//parameterized constructor call
Blog obj1 = new Blog("theitroad", 100);
obj1.display();
//Copy-Constructor call
Blog obj2 = new Blog(obj1);
obj2.display();
}
}
复制构造函数和display()方法的输出
复制构造函数输出
在上面的代码中,我们看到了复制构造函数中'this'关键字的应用和方法display()。
使用this关键字调用构造函数
让我们看一个使用此关键字调用构造函数的示例。
package com.theitroad.examples;
public class Data {
Data() {
System.out.println("default constructor");
}
Data(int i) {
this();
System.out.println("int parameter constructor");
}
Data(String s) {
this(10);
System.out.println("string parameter constructor");
}
public static void main(String[] args) {
Data d = new Data(20);
System.out.println("------");
Data d1 = new Data("Hi");
}
}
输出:
default constructor int parameter constructor ----- default constructor int parameter constructor string parameter constructor
使用this关键字调用对象方法
让我们看一个使用此关键字调用对象方法的示例。
package com.theitroad.examples;
public class Data {
Data() {
this.foo();
}
private void foo() {
System.out.println("foo method");
}
public static void main(String[] args) {
Data d = new Data();
}
}
在方法参数中使用this关键字
package com.theitroad.examples;
public class Data {
private int id;
public Data(int id) {
this.id = id;
bar(this);
}
private void bar(Data data) {
System.out.println(data.id);
}
public static void main(String[] args) {
Data d = new Data(20);
}
}
Java this关键字返回当前对象
让我们看一个简单的示例,在该示例中,我们将使用此关键字从方法中返回当前对象。
private Data bar(Data data) {
System.out.println(data.id);
return this;
}

