所以上面的例子打印順序應(yīng)該是這樣的:
parent static block 父類Static
child static block 子類static
parent block 父類缺省{}
parent constructor 父類構(gòu)造函數(shù)
child block子類缺省{}
child constructor子類構(gòu)造函數(shù)
class Parent{
static String name = "hello";
static {
System.out.println("parent static block");
}
{
System.out.println("parent block");
}
public Parent(){
System.out.println("parent constructor");
}
}
class Child extends Parent{
static String childName = "hello";
static {
System.out.println("child static block");
}
{
System.out.println("child block");
}
public Child(){
System.out.println("child constructor");
}
}
public class StaticIniBlockOrderTest {
public static void main(String[] args) {
new Child();//語句(*)
}
}
相關(guān)推薦:
2010年9月計算機等級考試精華備考資料匯總 2010計算機等考二級Java練習(xí)題及答案匯總