《大话设计模式》--模板模式
生活随笔
收集整理的這篇文章主要介紹了
《大话设计模式》--模板模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:相同的兩份試卷,甲乙兩個人做,答案不同
public class TestPager {public void question() {System.out.println("題目:答案是A、B、C、D中哪一個?");System.out.println("答案:" + answer());}protected String answer() {return "";} } public class TestPagerA extends TestPager {@Overrideprotected String answer() {return "A";} }public class TestPagerB extends TestPager {@Overrideprotected String answer() {return "B";} } public class Test {public static void main(String args[]) {System.out.println("甲的試卷");TestPager studentA = new TestPagerA();studentA.question();System.out.println("乙的試卷");TestPager studentB = new TestPagerB();studentB.question();} }?
打印結果:
甲的試卷 題目:答案是A、B、C、D中哪一個? 答案:A 乙的試卷 題目:答案是A、B、C、D中哪一個? 答案:B?
這其實就是通過面向對象的三大特性實現代碼的復用,使重復代碼降到最低
轉載于:https://www.cnblogs.com/anni-qianqian/p/7424134.html
總結
以上是生活随笔為你收集整理的《大话设计模式》--模板模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Activity中 onResume和o
- 下一篇: CentOS配置静态IP