设计模式14-模板方法
来源:互联网 发布:用别人身份证开淘宝店 编辑:程序博客网 时间:2024/06/08 07:18
简介
一个抽象类中,有一个主方法,再定义1…n个方法,可以是抽象的,也可以是实际的方法,定义一个类,继承该抽象类,重写抽象方法,通过调用抽象类,实现对子类的调用
抽象父类
public abstract class AbstractCalculator { /*主方法,实现对本类其它方法的调用*/ public final int calculate(String exp,String opt){ int array[] = split(exp,opt); return calculate(array[0],array[1]); } /*被子类重写的方法*/ abstract public int calculate(int num1,int num2); public int[] split(String exp,String opt){ String array[] = exp.split(opt); int arrayInt[] = new int[2]; arrayInt[0] = Integer.parseInt(array[0]); arrayInt[1] = Integer.parseInt(array[1]); return arrayInt; } }
子类
public class Plus extends AbstractCalculator { @Override public int calculate(int num1,int num2) { return num1 + num2; } }
使用
public class StrategyTest { public static void main(String[] args) { String exp = "8+8"; AbstractCalculator cal = new Plus(); int result = cal.calculate(exp, "\\+"); System.out.println(result); } }
备注
使用者调用父类,父类调用子类
其他设计模式http://blog.csdn.net/qq_23370223/article/category/7193147
阅读全文
0 0
- 设计模式14-模板方法
- 模板方法设计模式
- 模板方法设计模式
- 模板方法设计模式
- 模板方法设计模式
- 【设计模式】模板方法
- 模板方法设计模式
- 设计模式--模板方法
- 模板方法设计模式
- 设计模式-模板方法
- 设计模式-模板方法
- 模板方法设计模式
- 模板方法设计模式
- 模板方法设计模式
- 模板方法设计模式
- 模板方法设计模式
- 模板方法设计模式
- 设计模式-模板方法
- kamon文档翻译(一)
- 【转】Kubernetes高可用负载均衡与集群外服务访问实践
- 在线安装常用eclipse插件
- Eclipse 全文搜索
- Stata连享会:一个博士生该掌握哪些工具(武器)?
- 设计模式14-模板方法
- ci多环境配置config
- Web性能优化与Http2
- iOS mac上用终端写代码并调试
- 字符编码问题
- java基础补全计划-01-下载jdk和配置环境变量
- Mongodb Mysql 小测试 (To Be Continued)
- Myeclipse-常见问题第1篇-Myeclipse 10.7.1及以下破解补丁和破解教程
- android SharedPreferences保存list数据