首頁  >  文章  >  类库下载  >  java物件初始化的順序

java物件初始化的順序

高洛峰
高洛峰原創
2016-10-15 17:06:551914瀏覽

Java程式碼 

public class Son extends Father {  
  
    String value = null;//2  
  
    public Son() {  
        super();  //1  
        System.out.println("Son:  " + value);//3  
    }  
  
    public static void main(final String[] args) {  
        new Son();  
    }  
}  
  
  
class Father {  
  
    public Father() {  
        if (this instanceof Son) {  
            Son lower = (Son) this;  
            lower.value = "test";  
        }  
    }  
}  
  
  
class Father {  
  
    public Father() {  
        if (this instanceof Son) {  
            Son lower = (Son) this;  
            lower.value = "test";  
        }  
    }  
}

下載

這個的結果是null 
步驟1  設定為test 
步驟2  設定為null 
步驟3 列印
步驟1設定為test 
步驟2  不做任何事情,因為已經有值了,不用設定為預設的null值了 

步驟3 列印出來null 


所以  一個欄位不設定值和設定為null  是有區別的。 


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn