構(gòu)造方法中為final修飾的屬性賦值
public class Father {
final int g;
public Father(){
g=4;
System.out.println("father1類(lèi)無(wú)參數(shù)構(gòu)造方法已執(zhí)行");
System.out.println();
}
public Father(String newSex,double newB1,float newD1){
System.out.println("father1類(lèi)有參構(gòu)造方法已執(zhí)行");
g=4;
System.out.println();
}
為什么在兩種構(gòu)造方法中都要為g賦值??不能只在一個(gè)構(gòu)造方法(無(wú)參數(shù)或有參數(shù))中賦值呢??
2015-08-19
構(gòu)造方法? 在實(shí)體初始化時(shí) 只會(huì)調(diào)用一個(gè)構(gòu)造方法, 只在一個(gè)中 初始化 g? 你調(diào)用別的構(gòu)造方法? g 是不會(huì)被初始化的