更新時間:2020-11-18 17:23:11 來源:動力節點 瀏覽1021次
this關鍵字是我們在學習Java中接觸的為數不多的十分重要的關鍵字,無論是在Java面試題中還是實際的項目中都會經常遇到,因此,this關鍵字是我們學習Java的必修課。
this關鍵字表示當前這個對象,也就是說當前誰調用該方法則這個對象就是誰。每個對象都可以使用this關鍵字訪問其自身的引用,this引用可以隱式的或顯示的調用該方法的對象的實例變量和其他方法,this的參考名稱并不屬于實體變量,事實上在調用本對象方法時,this當成一個隱形的參數傳給這個方法。
下面我們就來一一介紹this關鍵字的用法:
一、當成員變量和局部變量重名時,在方法中使用this時,表示的是該方法所在類中的成員變量。(this是當前對象自己)
public class Hello {
String s = "Hello";//這里的S與Hello()方法里面的成員變量重名
public Hello(String s) {
System.out.println("s = " + s);
System.out.println("1 -> this.s = " + this.s);
this.s = s;//把參數值賦給成員變量,成員變量的值改變
System.out.println("2 -> this.s = " + this.s);
}
public static void main(String[] args) {
Hello x = new Hello("HelloWorld!");
System.out.println("s=" + x.s);//驗證成員變量值的改變
}
}
結果為:s = HelloWorld!
1 -> this.s = Hello
2 ->this.s = HelloWorld!s=HelloWorld!
二、把自己當作參數傳遞時,也可以用this.(this作當前參數進行傳遞)
class A {
public A() {
new B(this).print();// 調用B的方法
}
public void print() {
System.out.println("HelloAA from A!");
}
}
class B {
A a;
public B(A a) {
this.a = a;
}
public void print() {
a.print();//調用A的方法
System.out.println("HelloAB from B!");
}
}
public class HelloA {
public static void main(String[] args) {
A aaa = new A();
aaa.print();
B bbb = new B(aaa);
bbb.print();
}
}
結果為:
HelloAA from A!
HelloAB from B!
HelloAA from A!
HelloAA from A!
HelloAB from B!
在這個例子中,對象A的構造函數中,用new B(this)把對象A自己作為參數傳遞給了對象B的構造函數。
三、當在匿名類中用this時,這個this則指的是匿名類或內部類本身。
這時如果我們要使用外部類的方法和變量的話,則應該加上外部類的類名。如:
public class HelloB {
int i = 1;
public HelloB() {
Thread thread = new Thread() {
public void run() {
for (int j=0;j<20;j++) {
HelloB.this.run();//調用外部類的方法
try {
sleep(1000);
} catch (InterruptedException ie) {
}
}
}
}; // 注意這里有分號
thread.start();
}
public void run() {
System.out.println("i = " + i);
i++;
}
public static void main(String[] args) throws Exception {
new HelloB();
}
}
在上面這個例子中, thread 是一個匿名類對象,在它的定義中,它的 run 函數里用到了外部類的 run 函數。這時由于函數同名,直接調用就不行了。這時有兩種辦法,一種就是把外部的 run 函數換一個名字,但這種辦法對于一個開發到中途的應用來說是不可取的。那么就可以用這個例子中的辦法用外部類的類名加上 this 引用來說明要調用的是外部類的方法 run。
四、在構造函數中,通過this可以調用同一類中別的構造函數。如:
public class ThisTest {
ThisTest(String str) {
System.out.println(str);
}
ThisTest() {
this("this測試成功!");
}
public static void main(String[] args) {
ThisTest thistest = new ThisTest();
}
}
為了更確切的說明this用法,另外一個例子為:
public class ThisTest {
private int age;
private String str;
ThisTest(String str) {
this.str=str;
System.out.println(str);
}
ThisTest(String str,int age) {
this(str);
this.age=age;
System.out.println(age);
}
public static void main(String[] args) {
ThisTest thistest = new ThisTest("this測試成功",25);
}
}
結果為:this測試成功25
值得注意的是:
1:在構造調用另一個構造函數,調用動作必須置于最起始的位置。
2:不能在構造函數以外的任何函數內調用構造函數。
3:在一個構造函數內只能調用一個構造函數。這一點在第二個構造方法內可以看到,第一個this(str),第二個為this.age=age;
五、this同時傳遞多個參數
public class TestClass {
int x;
int y;
static void showtest(TestClass tc) {//實例化對象
System.out.println(tc.x + " " + tc.y);
}
void seeit() {
showtest(this);
}
public static void main(String[] args) {
TestClass p = new TestClass();
p.x = 9;
p.y = 10;
p.seeit();
}
}
結果為:9 10
希望大家可以通過上面對this關鍵字的介紹和用法實例的學習,可以在今后的Java學習和編程中熟練使用this關鍵字。在本站的Java SE教程中有著對眾多Java關鍵字的全方位解讀,感興趣的小伙伴不容錯過哦。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習