1、子類實(shí)例化時(shí)會默認(rèn)調(diào)用父類無參構(gòu)造函數(shù),如果父類沒有無參構(gòu)造函數(shù),則需要子類構(gòu)造函數(shù)顯示調(diào)用父類有參構(gòu)造函數(shù)
2、關(guān)于模板方法
public class Father {
public void run() {
before();
say();
after();
}
private void before() {
System.out.println("before");
}
private void after() {
System.out.println("after");
}
public void say() {
System.out.println("father");
}
}
//繼承father并重寫say方法
public class Children extends Father {
@Override
public void say() {
System.out.println("hello children");
}
public static void main(String[] args) {
Children children = new Children();
children.run();
}
}
當(dāng)執(zhí)行children.run()方法時(shí),debug調(diào)試顯示調(diào)用過程是這樣的:
1、調(diào)用father.run()方法
2、調(diào)用father.before()方法
3、調(diào)用children.say()方法
4、調(diào)用father.after()方法