在java中,this關(guān)鍵字有很多種用法。 在java中,這是一個引用當(dāng)前對象的引用變量。
java this關(guān)鍵字的用法如下:
this關(guān)鍵字可用來引用當(dāng)前類的實例變量。this關(guān)鍵字可用于調(diào)用當(dāng)前類方法(隱式)。this()可以用來調(diào)用當(dāng)前類的構(gòu)造函數(shù)。this關(guān)鍵字可作為調(diào)用方法中的參數(shù)傳遞。this關(guān)鍵字可作為參數(shù)在構(gòu)造函數(shù)調(diào)用中傳遞。this關(guān)鍵字可用于從方法返回當(dāng)前類的實例。建議:如果你是java初學(xué)者,只學(xué)習(xí)
this關(guān)鍵字的前三個用法就可以了。

this關(guān)鍵字可以用來引用當(dāng)前類的實例變量。如果實例變量和參數(shù)之間存在歧義,則 this 關(guān)鍵字可用于明確地指定類變量以解決歧義問題。
了解沒有 this 關(guān)鍵字的問題
下面先來理解一個不使用 this 關(guān)鍵字的示例:
class Student {
int rollno;
String name;
float fee;
Student(int rollno, String name, float fee) {
rollno = rollno;
name = name;
fee = fee;
}
void display() {
System.out.println(rollno + " " + name + " " + fee);
}
}
class TestThis1 {
public static void main(String args[]) {
Student s1 = new Student(111, "ankit", 5000f);
Student s2 = new Student(112, "sumit", 6000f);
s1.display();
s2.display();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
0 null 0.0
0 null 0.0
在上面的例子中,參數(shù)(形式參數(shù))和實例變量(rollno和name)是相同的。 所以要使用this關(guān)鍵字來區(qū)分局部變量和實例變量。
使用 this 關(guān)鍵字解決了上面的問題
class Student {
int rollno;
String name;
float fee;
Student(int rollno, String name, float fee) {
this.rollno = rollno;
this.name = name;
this.fee = fee;
}
void display() {
System.out.println(rollno + " " + name + " " + fee);
}
}
class TestThis2 {
public static void main(String args[]) {
Student s1 = new Student(111, "ankit", 5000f);
Student s2 = new Student(112, "sumit", 6000f);
s1.display();
s2.display();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
111 ankit 5000
112 sumit 6000
如果局部變量(形式參數(shù))和實例變量不同,則不需要像下面的程序一樣使用this關(guān)鍵字:
不需要 this 關(guān)鍵字的程序示例
class Student {
int rollno;
String name;
float fee;
Student(int r, String n, float f) {
rollno = r;
name = n;
fee = f;
}
void display() {
System.out.println(rollno + " " + name + " " + fee);
}
}
class TestThis3 {
public static void main(String args[]) {
Student s1 = new Student(111, "ankit", 5000f);
Student s2 = new Student(112, "sumit", 6000f);
s1.display();
s2.display();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
111 ankit 5000
112 sumit 6000
對變量使用有意義的名稱是一種好的編程習(xí)慣。所以使用相同名稱的實例變量和參數(shù),并且總是使用
this關(guān)鍵字。
可以使用this關(guān)鍵字調(diào)用當(dāng)前類的方法。如果不使用this關(guān)鍵字,編譯器會在調(diào)用方法時自動添加此 this 關(guān)鍵字。我們來看看這個例子。
執(zhí)行上面代碼輸出結(jié)果如下 -
hello n
hello m
this()構(gòu)造函數(shù)調(diào)用可以用來調(diào)用當(dāng)前類的構(gòu)造函數(shù)。 它用于重用構(gòu)造函數(shù)。 換句話說,它用于構(gòu)造函數(shù)鏈接。
從參數(shù)化構(gòu)造函數(shù)調(diào)用默認構(gòu)造函數(shù):
class A {
A() {
System.out.println("hello a");
}
A(int x) {
this();
System.out.println(x);
}
}
class TestThis5 {
public static void main(String args[]) {
A a = new A(10);
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
hello a
10
從默認構(gòu)造函數(shù)調(diào)用參數(shù)化構(gòu)造函數(shù):
class A {
A() {
this(5);
System.out.println("hello a");
}
A(int x) {
System.out.println(x);
}
}
class TestThis6 {
public static void main(String args[]) {
A a = new A();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
5
hello a
this()構(gòu)造函數(shù)調(diào)用用于從構(gòu)造函數(shù)重用構(gòu)造函數(shù)。 它維護構(gòu)造函數(shù)之間的鏈,即它用于構(gòu)造函數(shù)鏈接??纯聪旅娼o出的示例,顯示this關(guān)鍵字的實際使用。
class Student {
int rollno;
String name, course;
float fee;
Student(int rollno, String name, String course) {
this.rollno = rollno;
this.name = name;
this.course = course;
}
Student(int rollno, String name, String course, float fee) {
this(rollno, name, course);// reusing constructor
this.fee = fee;
}
void display() {
System.out.println(rollno + " " + name + " " + course + " " + fee);
}
}
class TestThis7 {
public static void main(String args[]) {
Student s1 = new Student(111, "ankit", "java");
Student s2 = new Student(112, "sumit", "java", 6000f);
s1.display();
s2.display();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
111 ankit java null
112 sumit java 6000
注意:調(diào)用
this()必須是構(gòu)造函數(shù)中的第一個語句。
下面示例為不把 this() 語句放在第一行,因此編譯不通過。
class Student {
int rollno;
String name, course;
float fee;
Student(int rollno, String name, String course) {
this.rollno = rollno;
this.name = name;
this.course = course;
}
Student(int rollno, String name, String course, float fee) {
this.fee = fee;
this(rollno, name, course);// C.T.Error
}
void display() {
System.out.println(rollno + " " + name + " " + course + " " + fee);
}
}
class TestThis8 {
public static void main(String args[]) {
Student s1 = new Student(111, "ankit", "java");
Student s2 = new Student(112, "sumit", "java", 6000f);
s1.display();
s2.display();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
Compile Time Error: Call to this must be first statement in constructor
this關(guān)鍵字也可以作為方法中的參數(shù)傳遞。 它主要用于事件處理。 看看下面的一個例子:
class S2 {
void m(S2 obj) {
System.out.println("method is invoked");
}
void p() {
m(this);
}
public static void main(String args[]) {
S2 s1 = new S2();
s1.p();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
method is invoked
這個應(yīng)用程序可以作為參數(shù)傳遞:
在事件處理(或)的情況下,必須提供一個類的引用到另一個。 它用于在多個方法中重用一個對象。
也可以在構(gòu)造函數(shù)中傳遞this關(guān)鍵字。 如果必須在多個類中使用一個對象,可以使用這種方式。 看看下面的一個例子:
class B {
A4 obj;
B(A4 obj) {
this.obj = obj;
}
void display() {
System.out.println(obj.data);// using data member of A4 class
}
}
class A4 {
int data = 10;
A4() {
B b = new B(this);
b.display();
}
public static void main(String args[]) {
A4 a = new A4();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
10
可以從方法中 this 關(guān)鍵字作為語句返回。 在這種情況下,方法的返回類型必須是類類型(非原始)。 看看下面的一個例子:
作為語句返回的語法
return_type method_name(){
return this;
}
從方法中返回為語句的 this 關(guān)鍵字的示例
class A {
A getA() {
return this;
}
void msg() {
System.out.println("Hello java");
}
}
class Test1 {
public static void main(String args[]) {
new A().getA().msg();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
Hello java
驗證 this 關(guān)鍵字
現(xiàn)在來驗證 this 關(guān)鍵字引用當(dāng)前類的實例變量。 在這個程序中將打印參考變量,這兩個變量的輸出是相同的。
class A5 {
void m() {
System.out.println(this);// prints same reference ID
}
public static void main(String args[]) {
A5 obj = new A5();
System.out.println(obj);// prints the reference ID
obj.m();
}
}
執(zhí)行上面代碼輸出結(jié)果如下 -
A5@22b3ea59
A5@22b3ea59