Java 中这个关键字有什么用?
Java 中的 "this" 关键字是对当前类对象的引用。使用它,您可以引用类的字段、方法或构造函数。
使用 "this" 关键字引用字段
如上所述,您可以使用 "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[]) { //Reading values from user 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(); //Calling the setter and getter methods 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" 关键字引用构造函数
您还可以使用 "this" 关键字(显式地)从另一个构造函数中引用/调用类的构造函数。
示例
在下面的示例中,Student 类有两个私有变量 name 和 age。在这里,我们编写了四个构造函数:一个接受两个变量的构造函数,一个只接受 name 的构造函数,一个只接受 age 的构造函数,一个不接受任何变量的构造函数。
在每个构造函数中,我们都使用 this 关键字调用参数化构造函数(接受两个变量的构造函数)。
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[]) { //Reading values from user 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(" "); //Calling the constructor that accepts both values System.out.println("Display method of constructor that accepts both values: "); new StudentData(name, age).display(); System.out.println(" "); //Calling the constructor that accepts name System.out.println("Display method of constructor that accepts only name:"); new StudentData(name).display(); System.out.println(" "); //Calling the constructor that accepts age System.out.println("Display method of constructor that accepts only age: "); new StudentData(age).display(); System.out.println(" "); //Calling the default constructor 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 Display method of constructor that accepts only name: 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
使用 "this" 关键字引用方法
您还可以使用 this 从另一个实例方法中调用(实例)方法。
示例
在下面的示例中,Student 类有一个私有变量 name,以及 setter 和 getter 方法,使用 setter 方法我们为 name 变量赋值。并且我们从名为 display() 的实例方法中使用 "this" 关键字调用了 getter 方法。
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
广告