static void Main(string[] args) { //虚方法、抽象类、接口 //面向对象计算器 案例 while (true) { Console.WriteLine("请输入第一个数字"); double n1 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("请输入第二个数字"); double n2 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("请输入运算符"); string opera = Console.ReadLine(); CalFather cal = GetCal(opera, n1, n2); double res = cal.GetResult(); Console.WriteLine(res); Console.ReadKey(); } }
public static CalFather GetCal(string opear, double n1, double n2) { CalFather cal = null; switch (opear) { case "+": cal = new Add(n1, n2); break; case "-": cal = new Sub(n1, n2); break; case "*": cal = new Cheng(n1, n2); break; case "/": cal = new Chu(n1, n2); break; } return(cal); }