static void Run() { Opration opration = new Opration(); while (true) { try { //获取输入 Console.WriteLine("请输入数字A: "); string strNumberA = Console.ReadLine(); Console.WriteLine("请输入运算符号(+,-,*,/): "); string strOprate = Console.ReadLine(); Console.WriteLine("请输入数字B: "); string strNumberB = Console.ReadLine(); //工厂根据输入生产出实例 opration = OprationFactory.CreateOperate(strOprate); opration.NumberA = Convert.ToDouble(strNumberA); opration.NumberB = Convert.ToDouble(strNumberB); Console.WriteLine("结果:" + opration.GetResult()); } catch (Exception ex) { Console.WriteLine("输入错误:" + ex.Message); } } }
public static Opration CreateOperate(string operate) { Opration operation = null; switch (operate) { case "+": operation = new OprationAdd(); break; case "-": operation = new OprationSub(); break; case "*": operation = new OprationMul(); break; case "/": operation = new OprationDiv(); break; default: throw new Exception("运算符号未注册!"); } return(operation); }