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
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP