using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Operation op = GetOP("+");
op.NumberA = 5;
op.NumberB = 6;
Console.WriteLine(op.GetResult());
}
public static Operation GetOP(string ff)
{
Operation op = null;
switch (ff)
{
case "+":op = new Jia();break;
case "-":op = new Jian();break;
case "*":op = new Chen();break;
case "/":op = new Chen();break;
default:
break;
}
return op;
}
}
public abstract class Operation
{
public double NumberA { get; set; }
public double NumberB { get; set; }
public virtual double GetResult()
{
double result = 0;
return result;
}
}
class Jia : Operation
{
public override double GetResult()
{
double result = NumberA+NumberB;
return result;
}
}
class Jian : Operation
{
public override double GetResult()
{
double result = NumberA + NumberB;
return result;
}
}
class Chen : Operation
{
public override double GetResult()
{
return NumberA - NumberB;
}
}
class Chu : Operation
{
public override double GetResult()
{
if (NumberB == 0)
throw new Exception("禁止除以0");
return NumberA / NumberB;
}
}
}
计算器面向对象
最新推荐文章于 2023-07-03 21:07:47 发布