23、解释器模式(Interpreter)
来源:互联网 发布:三阶魔方淘宝 编辑:程序博客网 时间:2024/06/01 21:46
解释器模式是我们暂时的最后一讲,一般主要应用在OOP开发中的编译器的开发中,所以适用面比较窄。最后输出正确的结果:3。
Context类是一个上下文环境类,Plus和Minus分别是用来计算的实现,代码如下:
[java] view plaincopy
- public interface Expression {
- public int interpret(Context context);
- }
[java] view plaincopy
- public class Plus implements Expression {
- @Override
- public int interpret(Context context) {
- return context.getNum1()+context.getNum2();
- }
- }
[java] view plaincopy
- public class Minus implements Expression {
- @Override
- public int interpret(Context context) {
- return context.getNum1()-context.getNum2();
- }
- }
[java] view plaincopy
- public class Context {
- private int num1;
- private int num2;
- public Context(int num1, int num2) {
- this.num1 = num1;
- this.num2 = num2;
- }
- public int getNum1() {
- return num1;
- }
- public void setNum1(int num1) {
- this.num1 = num1;
- }
- public int getNum2() {
- return num2;
- }
- public void setNum2(int num2) {
- this.num2 = num2;
- }
- }
[java] view plaincopy
- public class Test {
- public static void main(String[] args) {
- // 计算9+2-8的值
- int result = new Minus().interpret((new Context(new Plus()
- .interpret(new Context(9, 2)), 8)));
- System.out.println(result);
- }
- }
阅读全文
0 0
- 23、解释器模式(Interpreter)
- 23、解释器模式(Interpreter)
- 23、解释器模式(Interpreter)
- 解释器模式(Interpreter)
- Interpreter(解释器模式)
- 解释器模式(Interpreter)
- Interpreter(解释器)模式
- 解释器模式(Interpreter)
- 解释器(interpreter)模式
- 解释器模式(Interpreter)
- 解释器模式(Interpreter)
- 解释器模式(Interpreter)
- 解释器模式(Interpreter)
- 解释器模式(Interpreter)
- 解释器模式(Interpreter)
- 解释器模式(interpreter)
- 解释器模式(Interpreter)
- 解释器(Interpreter )模式
- linux(centos7)上安装使用RabbitMQ
- tensorflow 问题与解决
- Vuex 学习笔记
- 聊聊近况
- QT 进行md5加密
- 23、解释器模式(Interpreter)
- 详解Mysql中的JSON系列操作函数
- 笔记40 | Android通讯之Intent的简单使用
- Android图片加载框架最全解析(三),深入探究Glide的缓存机制
- Python多版本pip安装库的问题
- C++类静态成员与类静态成员函数
- 开启mysql远程权限
- 模型评估与选择(番外2)-Stanford机器学习笔记-6.
- 子类继承父类中的static模块、构造方法执行顺序及java多态性