namespace TEST { //用于生成一个对应的操作类,这个工厂只是用来产生操作类的,不做其他只用 public class Factory { public Operate GetOpetration(string mark) { switch(mark) { case "+": return new OperateAdd(); break; case "-": return new OperateSub(); break; case "*": return new OperateMultiply(); break; case "/": return new OperateDivision(); break; default: return null; break; } } } //这个类是用来执行相关的操作的 public abstract class Operate { public double operateTwo { get; set; } public double operateOne { get; set; } public abstract string Opetration(); } public class OperateAdd:Operate { public override string Opetration() { return Convert.ToString(base.operateOne + base.operateTwo); } } public class OperateSub : Operate { public override string Opetration() { return Convert.ToString(this.operateOne - this.operateTwo); } } public class OperateMultiply : Operate { public override string Opetration() { return Convert.ToString(this.operateOne*this.operateTwo); } } public class OperateDivision : Operate { public override string Opetration() { if (this.operateTwo != 0) return Convert.ToString(this.operateOne / this.operateTwo); else return "input error"; } } class Program { static void Main(string[] args) { try { double num1=Convert.ToDouble( Console.ReadLine()); double num2 =Convert.ToDouble( Console.ReadLine()); string op = Convert.ToString(Console.ReadLine()); var factory = new Factory(); var Opetration = factory.GetOpetration(op); Opetration.operateOne = num1; Opetration.operateTwo = num2; Console.WriteLine(Opetration.Opetration()); Console.Read(); Console.Read(); } catch (Exception ex) { Console.WriteLine("程序出错"); } } } }