Java面向?qū)ο?/h2>

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()方法
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容