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 打印出來null
如果 不是 String value = null ; 只是 String value; 下載
步驟1 設置爲test
步驟2 不做任何事情,因爲已經有值了,不用設置爲默認的null值了
步驟3 打印出來null
所以 一個字段不設置值 和 設置爲null 是有區別的。