模板方法模式
来源:互联网 发布:centos编译安装php7.0 编辑:程序博客网 时间:2024/06/14 02:15
一个抽象类中,有一个主方法,再定义1...n个方法,可以是抽象的,也可以是实际的方法,定义一个类,继承该抽象类,重写抽象方法,通过调用抽象类,实现对子类的调用。
/** * * @author: muyichun * @date : 2016年3月23日15:07:32 * @function: 模板方法模式 */public class Main{public static void main(String[] args) { String exp = "8+8"; AbstractCalculator cal = new Plus(); int result = cal.calculate(exp, "\\+"); System.out.println(result);}}abstract class AbstractCalculator{//主方法,实现对本类其它方法的调用public final int calculate(String exp, String opt){int array[] = split(exp, opt);return calculate(array[0], array[1]);}//被子类重写的方法public abstract 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;}}class Plus extends AbstractCalculator{@Overridepublic int calculate(int num1, int num2) {return num1 + num2;}}——贴上自己喜欢的代码!
0 0
- 深入浅出模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法设计模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- TemplateMethod模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 汇编程序与编译程序区别 assembler compiler
- ExtJs4 笔记(2) ExtJs对js基本语法扩展支持
- JQuery上传插件Uploadify使用详解
- iOS 点击按钮实现拨打电话,发送短信功能
- mysql 主主互备
- 模板方法模式
- Cg入门17:Fragment shader - 片段级光照(添加阴影)
- 后台传xml到jsp页面处理
- android shape.xml 文件使用
- Glide 入门到精通之九——SimpleTarget 和 ViewTarget 用于自定义视图类
- android中实现返回首页功能
- forName与loadClass的区别
- 前段规范 2016.5.3
- ad