标签:style blog color io ar div log new
1 /** 2 * 面向对象(运算类) 3 * 简易计算器 4 * @author Monica 5 * 6 */ 7 public class Operation { 8 private double numberA = 0; 9 private double numberB = 0; 10 11 //虚函数 12 public double GetResult() { 13 double result = 0; 14 return result; 15 } 16 17 public double getNumberA() { 18 return numberA; 19 } 20 21 public void setNumberA(double numberA) { 22 this.numberA = numberA; 23 } 24 25 public double getNumberB() { 26 return numberB; 27 } 28 29 public void setNumberB(double numberB) { 30 this.numberB = numberB; 31 } 32 }
1 /** 2 * 加法 3 * @author Monica 4 * 5 */ 6 public class OperationAdd extends Operation { 7 8 /** 9 * 继承运算类 10 */ 11 @Override 12 public double GetResult() { 13 double result = 0; 14 result = getNumberA() + getNumberB(); 15 return super.GetResult(); 16 } 17 }
1 /** 2 * 简单工厂模式 3 * @author Administrator 4 * 5 */ 6 public class OperationFactory { 7 8 public static Operation createOperate(char operate) { 9 Operation oper = null; 10 switch (operate) { 11 case ‘+‘: 12 oper = new OperationAdd(); 13 break; 14 } 15 return oper; 16 } 17 }
1 public class Main { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 Operation oper; 8 oper = OperationFactory.createOperate(‘+‘); 9 oper.setNumberA(1); 10 oper.setNumberB(2); 11 double result = oper.GetResult(); 12 } 13 14 }
学习大话设计模式01_简单工厂模式(简易计算器),布布扣,bubuko.com
标签:style blog color io ar div log new
原文地址:http://www.cnblogs.com/qqiaoq/p/3921599.html