Java中的“ this”关键字是对当前类对象的引用。使用它,您可以引用类的字段,方法或构造函数。
如前 ,您可以使用“ this” 关键字从实例方法或构造函数中引用类的实例字段/变量。
即如果一个方法的名称相同的实例变量一个局部变量的话,你可以使用从局部变量区分实例变量这个 它。
在下面的Java示例中,Student类具有两个私有字段name和age,分别具有setter和getter方法。setter方法具有与实例变量同名的局部变量,我们使用“ this”关键字来区分它们。
public class Student {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void display() {
System.out.println("name: "+getName());
System.out.println("age: "+getAge());
}
public static void main(String args[]) {
//从用户读取值
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the student: ");
String name = sc.nextLine();
System.out.println("Enter the age of the student: ");
int age = sc.nextInt();
//调用setter和getter方法
ThisExample obj = new ThisExample();
obj.setName(name);
obj.setAge(age);
obj.display();
}
}输出结果
Enter the name of the student: Krishna Enter the age of the student: 22 name: Krishna age: 22
您也可以使用“ this”关键字(明确地)从另一个构造函数引用/调用类的构造函数。
在下面的示例中,Student类具有两个私有变量name和age。在本文中,我们正在编写四个构造:一个接受两个变量,一个仅接受名称,一个仅接受年龄,一个不接受年龄。
在每个构造函数中,我们都使用此 关键字调用参数化的构造函数(接受两个变量)。
public class StudentData {
private String name;
private int age;
public StudentData(String name, int age){
this.name = name;
this.age = age;
}
public StudentData(){
this(null, 0);
}
public StudentData(String name) {
this(name, 0);
}
public StudentData(int age) {
this(null, age);
}
public void display(){
System.out.println("Name of the Student: "+this.name );
System.out.println("Age of the Student: "+this.age );
}
public static void main(String args[]) {
//从用户读取值
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the student: ");
String name = sc.nextLine();
System.out.println("Enter the age of the student: ");
int age = sc.nextInt();
System.out.println(" ");
//调用接受两个值的构造函数
System.out.println("Display method of constructor that accepts both values: ");
new StudentData(name, age).display();
System.out.println(" ");
//调用接受名称的构造函数
System.out.println("仅接受名称的构造方法的显示方法:");
new StudentData(name).display();
System.out.println(" ");
//调用接受年龄的构造函数
System.out.println("Display method of constructor that accepts only age: ");
new StudentData(age).display();
System.out.println(" ");
//调用默认构造函数
System.out.println("Display method of default constructor: ");
new StudentData().display();
}
}输出结果
Enter the name of the student: Krishna Enter the age of the student: 22 Display method of constructor that accepts both values: Name of the Student: Krishna Age of the Student: 22 仅接受名称的构造方法的显示方法: Name of the Student: Krishna Age of the Student: 0 Display method of constructor that accepts only age: Name of the Student: null Age of the Student: 22 Display method of default constructor: Name of the Student: null Age of the Student: 0
您还可以使用它从另一个实例方法调用(实例)方法。
在下面的示例中,Student类具有一个私有变量名称,以及setter和getter方法,使用setter方法,我们已经为name变量分配了值。并且我们从名为的实例方法中使用“ this”关键字调用了getter方法display()。
public class ThisExample_Method {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void display() {
System.out.println("name: "+this.getName());
}
public static void main(String args[]) {
ThisExample_Method obj = new ThisExample_Method();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the student: ");
String name = sc.nextLine();
obj.setName(name);
obj.display();
}
}输出结果
Enter the name of the student: Krishna name: Krishna