C#基礎之簡單工廠模式計算器

来源:http://www.cnblogs.com/zzzhjy/archive/2017/04/03/6663519.html
-Advertisement-
Play Games

類庫:.dll文件,使用類庫來封裝常用的功能,無法單獨運行。 abstact class Calculator類{欄位Num1,欄位Num2,abstract int GetResult()方法} Add類:Calculator類{override int GetResult()返回Num1+Num ...


類庫:.dll文件,使用類庫來封裝常用的功能,無法單獨運行。

abstact class Calculator類{欄位Num1,欄位Num2,abstract int GetResult()方法}

Add類:Calculator類{override int GetResult()返回Num1+Num2}

Sub類{...}

Multi{...}

divi {...}

工廠類:-------------------------------------------------------------------------------

public class CaculatorFactory
    {
       public static Calc CreateCalc(string oper)
       {
           Calc calc = null;
           switch (oper)
           {
               case "+":
                  calc=new Add();
                   break;
               case "-":
                  calc=new Sub();
                   break;
               case "*":
                   calc=new Multi();
                   break;
               case "/":
                   calc=new Divi();
                   break;
           }
           return calc;
       }
    }

Main()

{

  Calculator calc = CalculatorFactory.CreateCalc(oper);

  calc.Num1=  ;

  calc.Num2=  ;

  int result = calc.GetResult();

}

 

完成

  1 偷的代碼-----------------------------------------------------------------------
  2 Add.cs
  3 namespace ProOperation
  4 {
  5     public class Add : Operation
  6     {
  7         public Add(int n1, int n2)
  8             : base(n1, n2)
  9         { }
 10         public override int GetResult()
 11         {
 12             return this.NumberOne + this.NumberTwo;
 13         }
 14     }
 15 }
 16 
 17 Cheng.cs
 18 namespace ProOperation
 19 {
 20     public  class Cheng:Operation
 21     {
 22         public Cheng(int n1, int n2)
 23             : base(n1, n2)
 24         { }
 25 
 26         public override int GetResult()
 27         {
 28             return this.NumberOne * this.NumberTwo;
 29         }
 30     }
 31 }
 32 
 33 Chu.cs
 34 namespace ProOperation
 35 {
 36     public class Chu:Operation
 37     {
 38         public Chu(int n1, int n2)
 39             : base(n1, n2)
 40         { }
 41 
 42         public override int GetResult()
 43         {
 44             return this.NumberOne / this.NumberTwo;
 45         }
 46     }
 47 }
 48 
 49 Sub.cs
 50 namespace ProOperation
 51 {
 52     public class Sub : Operation
 53     {
 54         public Sub(int n1, int n2):base(n1,n2)
 55         { }
 56 
 57         public override int GetResult()
 58         {
 59             return this.NumberOne - this.NumberTwo;
 60         }
 61     }
 62 }
 63 
 64 Operation.cs
 65 namespace ProOperation
 66 {
 67     public abstract class Operation
 68     {
 69         public int NumberOne { get; set; }
 70         public int NumberTwo { get; set; }
 71         public Operation(int n1, int n2)
 72         {
 73             this.NumberOne = n1;
 74             this.NumberTwo = n2;
 75         }
 76         public abstract int GetResult();
 77     }
 78 }
 79 
 80 namespace Calculator
 81 {
 82     class Program
 83     {
 84         static void Main(string[] args)
 85         {
 86             Console.WriteLine("請輸入第一個數字");
 87             int n1 = Convert.ToInt32(Console.ReadLine());
 88             Console.WriteLine("請輸入第二個數字");
 89             int n2 = Convert.ToInt32(Console.ReadLine());
 90             Console.WriteLine("請輸入操作符");
 91             string oper = Console.ReadLine();
 92 
 93             //根據用戶輸入的操作符 返回算符的父類
 94 
 95             Operation operation = GetOperation(oper, n1, n2);
 96             if (operation != null)
 97             {
 98                 int result = operation.GetResult();
 99                 Console.WriteLine("{0}{1}{2}={3}", n1, oper, n2, result);
100             }
101             else
102             {
103                 Console.WriteLine("沒有你想要的運算符");
104             }
105             Console.ReadKey();
106         }
107 
108         static Operation GetOperation(string oper, int n1, int n2)
109         {
110             Operation operation = null;
111             switch (oper)
112             { 
113                 case "+":
114                     operation = new Add(n1, n2);
115                     break;
116                 case "-":
117                     operation = new Sub(n1, n2);
118                     break;
119                 case "*":
120                     operation = new Cheng(n1, n2);
121                     break;
122                 case "/":
123                     operation = new Chu(n1, n2);
124                     break;
125             }
126             return operation;
127         }
128     }
129 }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 界面http://localhost:你的伺服器/Code/index 實現步驟: 註冊賬號https://www.geetest.com 新增驗證 下載demo (url:http://docs.geetest.com/install/server/csharp/) 找到C#的SDK .dll g ...
  • 使用Microsoft.NETCore.Portable.Compatibility會破壞該類庫在Mono和Xamarin平臺的相容性 可能導致的問題 和 無法安裝程式包“Microsoft.NETCore.Jit 1.0.2” ...
  • 跨平臺系列彙總:http://www.cnblogs.com/dunitian/p/4822808.html#linux 在說正式步驟前先把準備工作做到位: 1.IP設置,這個因為是GUI的,手動設置一下就好了,如果想知道命令行的方式看這篇文章:(http://www.cnblogs.com/dun ...
  • 在游戲中,程式,美術,策劃甚至音效都是分工合作的。很多時候,對於unity3d中一堆英文,大家都會看得很鬱悶。尤其是不同的程式員,命名方式也不盡相同,甚至還是用拼音。因此,在腳本中增加一些中文顯示,就能夠很好地解決這個問題。 首先,unity中對於欄位(Field)已經有了很好的中文顯示方法[Hea ...
  • (四)建造者模式(Builder Pattern) 1.建造者模式(Builder Pattern)使用多個簡單的對象一步一步構建成一個複雜的對象。這種類型的設計模式屬於創建型模式,它提供了一種創建對象的最佳方式。 一個 Builder 類會一步一步構造最終的對象。該 Builder 類是獨立於其他 ...
  • (二)抽象工廠模式(Abstract Factory) 1.抽象工廠模式(Abstract Factory),提供了一個創建一系列相關或相互依賴對象的介面,而無需指定它們具體的類。 2.抽象工廠模式是圍繞一個超級工廠創建其他工廠。該超級工廠又稱為其他工廠的工廠。這種類型的設計模式屬於創建型模式,它提 ...
  • jdbc: java database connection,也就是java的資料庫連接。 作用: 完成資料庫數據和記憶體數據的交互。 為了屏蔽不同資料庫的差異,在記憶體和各種資料庫之間建立了一個介面標準。每個廠商按照介面的標準來實現介面類。 jdbc 是java連接資料庫的一套標準。該標準中定義了一系 ...
  • 創建用戶 ? 1 CREATE USER 'username'@'host' IDENTIFIED BY 'password'; ? 1 CREATE USER 'username'@'host' IDENTIFIED BY 'password'; ? 1 CREATE USER 'username ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...