![](https://i-blog.csdnimg.cn/blog_migrate/c845ad62ee8d62ff07a876f70311c763.png)
//运算抽象类
abstract class Operation
{
private double numberA;
private double numberB;
public double NumberA { get => numberA; set => numberA = value; }
public double NumberB { get => numberB; set => numberB = value; }
public virtual double GetResult()
{
double result = 0;
return result;
}
}
//“加”运算
class OperationAdd:Operation
{
public override double GetResult()
{
double result = 0;
result=NumberA+NumberB;
return result;
}
}
//"减"运算
class OperationSub:Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
//“乘”运算
class OperationMul:Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
//"除"运算
class OperationMul:Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
//运算工厂
class OperationFactory
{
public static Operation createOperate(string type)
{
Operation operation=null;
switch(type)
{
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationSub();
break;
case "*":
operation = new OperationMul();
break;
case "/":
operation = new OperationDiv();
break;
}
return operation;
}
}
//主函数
class Program
{
static void Main(string[] args)
{
Operation operation = OperationFactory.createOperate("+");
operation.NumberA = 10;
operation.NumberB = 20;
double result=operation.GetResult();
Console.WriteLine(result);
}
}
(完)