請說明代碼,無法理解main()方法中的test(3).x是什么意思class Test { int x=0; static int y=1; Test(){ this.y++; this.x+=++x; } Test(int x){ this(); System.out.println(x+this.x+this.y); } } public class MyClass { public static void main(String args[]) { System.out.println(new Test(3).x+" "+new Test(4).y); }}
2 回答

侃侃無極
TA貢獻2051條經(jīng)驗 獲得超10個贊
Test(3)
這意味著您正在調(diào)用參數(shù)化的構(gòu)造函數(shù)Test(int x)
,并將3的值傳遞給x。this
關(guān)鍵字用于引用該類的當(dāng)前對象。當(dāng)使用this.variable名稱時,表示在類的范圍內(nèi)引用了與當(dāng)前對象關(guān)聯(lián)的變量(正在使用“ new”關(guān)鍵字,例如創(chuàng)建新對象new Test(3).x
)。因此,參數(shù)化的構(gòu)造函數(shù)將相應(yīng)地被調(diào)用,并且其中包含的任何代碼將由編譯器相應(yīng)地解析。
添加回答
舉報
0/150
提交
取消