我首先糾正一下 pp不是指針
它是對象的一個實例.
pp.s是類的屬性
因為 你為 String s = "Hello "; 賦值為 "Hello "所以在
沒有 this.s=s 的這句代碼時屬性s的值肯定是"Hello"
詳細點給你解釋一下吧:
public class A
{
String s = "Hello "; //類的屬性
/*構造方法
*一般用來初始化
*此方法特點與類同名 A */
public A(String s)
{
/*此方法有一個參數(shù) String s (參數(shù)s與屬性s同名是個壞的習慣)
*由于參數(shù)與屬性同名
*所以要初始化屬性s,首先要先區(qū)分出參數(shù)s和屬性s (this關鍵字可以作到)*/
System.out.println( "s = " + s);//這個s是參數(shù)s所以打印的值應該是 "HelloWorld! "
System.out.println( "1 - > this.s = " + this.s);//這個是屬性s 值是 "Hello "
/*this翻譯成中文就是 '這里 '的意思this代表的是這個類
*this.s=s的意識是:把參數(shù)s賦給類的s屬性,
*(如果沒有this你知道那個s是那個s嗎?所以說參數(shù)s與屬性s同名是個壞的習慣*/
this.s=s; //賦值后屬性s變成 "HelloWorld! "
System.out.println( "2 - > this.s = " + this.s);//輸出屬性s此時打印 "HelloWorld! "
}
/*程序的入口*/
public static void main(String[] args)
{
A pp=new A( "HelloWorld! ");//new一個實例 系統(tǒng)自動調用構造函數(shù);
System.out.println(pp.s);//打印屬性s 此時值為 "HelloWorld! "
}
}
流程:1.程序的入口.
2.new pp 實例 自動的掉用構造函數(shù) A(String s);
3.執(zhí)行A方法的代碼
4.執(zhí)行System.out.println(pp.s);
1.讀萬卷書
2.行千里路
3.閱人無數(shù)