C#簡單工廠設(shè)計模式實現(xiàn)計算器

一野舶、首先創(chuàng)建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;
        }
    }
}

二涨薪、創(chuàng)建項目SimpleFactoryPatternCalculator,引入PlusOperation類庫炫乓,添加Operation命名空間

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("請輸入你的第一個數(shù)字");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("請輸入你的第二個數(shù)字");
            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;
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末刚夺,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子末捣,更是在濱河造成了極大的恐慌侠姑,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件箩做,死亡現(xiàn)場離奇詭異莽红,居然都是意外死亡,警方通過查閱死者的電腦和手機卒茬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進店門船老,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人圃酵,你說我怎么就攤上這事柳畔。” “怎么了郭赐?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵薪韩,是天一觀的道長。 經(jīng)常有香客問我捌锭,道長俘陷,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任观谦,我火速辦了婚禮拉盾,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘豁状。我一直安慰自己捉偏,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布泻红。 她就那樣靜靜地躺著夭禽,像睡著了一般。 火紅的嫁衣襯著肌膚如雪谊路。 梳的紋絲不亂的頭發(fā)上讹躯,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天,我揣著相機與錄音,去河邊找鬼潮梯。 笑死骗灶,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的秉馏。 我是一名探鬼主播矿卑,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼沃饶!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起轻黑,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤糊肤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后氓鄙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體馆揉,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年抖拦,在試婚紗的時候發(fā)現(xiàn)自己被綠了升酣。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡态罪,死狀恐怖噩茄,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情复颈,我是刑警寧澤绩聘,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站耗啦,受9級特大地震影響凿菩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜帜讲,卻給世界環(huán)境...
    茶點故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一衅谷、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧似将,春花似錦获黔、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至译红,卻和暖如春预茄,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工耻陕, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留拙徽,地道東北人。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓诗宣,卻偏偏與公主長得像膘怕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子召庞,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,612評論 2 350

推薦閱讀更多精彩內(nèi)容