public class Operation
{
public double NumberA=0;
public double NumberB=0;
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 OperationDiv:Operation
{
public class override double GetResult()
{
double result=0;
if(NumberB==0)
{
throw new Exception("除数不能为0");
}
result=NumberA/NumberB;
return result;
}
}
public class OperationFactory
{
public static Operation createOperate(string operate)
{
Operation oper=null;
switch(operate)
{
case "+":
oper=new OperationAdd();
break;
case "-":
oper =new OperationSub();
break;
case "*":
oper=new OperationMul();
break;
case "/":
oper=new OperationSub();
break;
}
return oper;
}
}
Operation oper;
oper=OperationFactory.createOperate("+");
oper.NumberA=1;
oper.NumberB=2;
double result=oper.GetResult();