《大话设计模式》--模板模式

摘要:
答案:A乙的试卷题目:答案是A、B、C、D中哪一个?

题目:相同的两份试卷,甲乙两个人做,答案不同

public classTestPager {
    public voidquestion() {
        System.out.println("题目:答案是A、B、C、D中哪一个?");
        System.out.println("答案:" +answer());
    }
    protectedString answer() {
        return "";
    }
}
public class TestPagerA extendsTestPager {
    @Override
    protectedString answer() {
        return "A";
    }
}
public class TestPagerB extendsTestPager {
    @Override
    protectedString answer() {
        return "B";
    }
}
public classTest {
    public static voidmain(String args[]) {
        System.out.println("甲的试卷");
        TestPager studentA = newTestPagerA();
        studentA.question();
        System.out.println("乙的试卷");
        TestPager studentB = newTestPagerB();
        studentB.question();
    }
}

打印结果:

甲的试卷
题目:答案是A、B、C、D中哪一个?
答案:A
乙的试卷
题目:答案是A、B、C、D中哪一个?
答案:B

这其实就是通过面向对象的三大特性实现代码的复用,使重复代码降到最低

免责声明:文章转载自《《大话设计模式》--模板模式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Scala学习2——idea配置scala环境,scala初步学习web的几种轮播下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章