更新時(shí)間:2022-06-09 10:58:40 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1421次
Java中的this關(guān)鍵字的定義是什么?動(dòng)力節(jié)點(diǎn)小編來告訴大家。
this與類屬性 (x) 一起使用:
public class Main {
int x;
// Constructor with a parameter
public Main(int x) {
this.x = x;
}
// Call the constructor
public static void main(String[] args) {
Main myObj = new Main(5);
System.out.println("Value of x = " + myObj.x);
}
}
this關(guān)鍵字指的是方法或構(gòu)造函數(shù)中的當(dāng)前對(duì)象。
關(guān)鍵字最常見的用途this是消除類屬性和同名參數(shù)之間的混淆(因?yàn)轭悓傩员环椒ɑ驑?gòu)造函數(shù)參數(shù)所遮蔽)。如果您在上面的示例中省略關(guān)鍵字,則輸出將是“0”而不是“5”。
this也可用于:
調(diào)用當(dāng)前類構(gòu)造函數(shù)
調(diào)用當(dāng)前類方法
返回當(dāng)前類對(duì)象
在方法調(diào)用中傳遞參數(shù)
在構(gòu)造函數(shù)調(diào)用中傳遞一個(gè)參數(shù)
以上就是關(guān)于“Java中的this關(guān)鍵字的定義”介紹,大家如果想了解更多相關(guān)知識(shí),不妨來關(guān)注一下動(dòng)力節(jié)點(diǎn)的Java教程,里面的課程內(nèi)容更加全面,相信對(duì)大家的學(xué)習(xí)一定會(huì)有所幫助的。
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)