博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《大话设计模式》--模板模式
阅读量:4308 次
发布时间:2019-06-06

本文共 892 字,大约阅读时间需要 2 分钟。

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

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 {    @Override    protected String answer() {        return "A";    }}public class TestPagerB extends TestPager {    @Override    protected 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

你可能感兴趣的文章
股票网格交易策略
查看>>
matplotlib绘图跳过时间段的处理方案
查看>>
vnpy学习_04回测评价指标的缺陷
查看>>
ubuntu终端一次多条命令方法和区别
查看>>
python之偏函数
查看>>
vnpy学习_06回测结果可视化改进
查看>>
读书笔记_量化交易如何建立自己的算法交易01
查看>>
设计模式03_工厂
查看>>
设计模式04_抽象工厂
查看>>
设计模式05_单例
查看>>
设计模式06_原型
查看>>
设计模式07_建造者
查看>>
设计模式08_适配器
查看>>
设计模式09_代理模式
查看>>
设计模式10_桥接
查看>>
设计模式11_装饰器
查看>>
设计模式12_外观模式
查看>>
设计模式13_享元模式
查看>>
设计模式14_组合结构
查看>>
设计模式15_模板
查看>>