C#简单工厂设计模式实现计算器

一、首先创建PlusOperation类库,其中包含抽象父类Operation,以及加、减、乘、除四个子类!
(1)父类Operation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NameSpaceOperation
{
    public abstract class Operation
    {
        public int NumberOne { get; set; }
        public int NubmerTwo { get; set; }
        public Operation(int a,int b)
        {
            this.NumberOne = a;
            this.NubmerTwo = b;

        }
        public abstract int GetResult();
    }
}
(2)加法子类Add
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NameSpaceOperation
{
    public class Add : Operation
    {
        public Add(int a, int b) : base(a, b)
        {
        }

        public override int GetResult()
        {
            return this.NumberOne + this.NubmerTwo;
        }
    }
}
(3)减法子类Sub
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NameSpaceOperation
{
    public class Sub : Operation
    {
        public Sub(int a, int b) : base(a, b)
        {
        }

        public override int GetResult()
        {
            return this.NumberOne - this.NubmerTwo;
        }
    }
}
(4)乘法子类Mul
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NameSpaceOperation
{
    public class Mul : Operation
    {
        public Mul(int a, int b) : base(a, b)
        {
        }

        public override int GetResult()
        {
            return this.NumberOne * this.NubmerTwo;
        }
    }
}
(5)除法子类Div
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace NameSpaceOperation
{
    public class Div : Operation
    {
        public Div(int a, int b) : base(a, b)
        {
        }

        public override int GetResult()
        {
            return this.NumberOne / this.NubmerTwo;
        }
    }
}
二、创建项目SimpleFactoryPatternCalculator,引入PlusOperation类库,添加NameSpaceOperation命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NameSpaceOperation;

namespace SimpleFactoryPatternCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入你的第一个数字");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入你的第二个数字");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入你的运算符");
            string strOper = Console.ReadLine();
            Operation oper = GetOperation(strOper,a, b);
            if (oper!=null)
            {
                int result = oper.GetResult();
                Console.WriteLine("{0}{1}{2}={3}",a,strOper,b,result);
            }
            else
            {
                Console.WriteLine("没有你需要的运算符");
            }

        }
        static Operation GetOperation(string oper,int a,int b)
        {
            Operation operation = null;
            switch (oper)
            {
                case "+":
                    operation = new Add(a, b);
                    break;
                case "-":
                    operation = new Sub(a, b);
                    break;
                case "*":
                    operation = new Mul(a, b);
                    break;
                case "/":
                    operation = new Div(a, b);
                    break;
            }
            return operation;
        }
    }
}
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值