溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#中的簡單工廠設計模式示例

發(fā)布時間:2020-07-10 02:47:08 來源:網(wǎng)絡 閱讀:430 作者:風信子0311 欄目:編程語言

這個是用面向?qū)ο蟮姆椒▉韺崿F(xiàn)加,減,乘,除的計算,使用了“簡單工廠的設計模式”。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 簡單公司實現(xiàn)計算1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入第一個數(shù)字:");
          double n1=  Convert.ToDouble( Console.ReadLine());
          Console.WriteLine("請輸入第二個數(shù)字:");
         double n2= Convert.ToDouble(Console.ReadLine());
         Console.WriteLine("請輸入一個操作符");
        string oper= Console.ReadLine();
        CalFather cal = Result(oper, n1, n2);
       double result= cal.GetResult();
       Console.WriteLine(result);
       Console.ReadKey();


        }

        /// <summary>
        /// 簡單工廠模式
        /// </summary>
        /// <param name="oper">傳入的操作符</param>
        /// <param name="n1">第一個運算的數(shù)字</param>
        /// <param name="n2">第二個運算的數(shù)字</param>
        /// <returns></returns>
        public static CalFather Result(string oper,double n1,double n2)
        {
            CalFather cal = null;
            switch (oper)
            {
                case "+": cal = new Add(n1, n2);
                    break;
                case "-": cal = new Sub(n1, n2);
                    break;
                case "*": cal = new Ride(n1, n2);
                    break;
                case "/":cal=new Chu(n1,n2);
                    break;
                default: Console.WriteLine("輸入有誤");
                    break;
            }

            return cal;
        }
    }
    /// <summary>
    /// 父類模型,用abstract抽象函數(shù)來實現(xiàn)多態(tài)
    /// </summary>
    public abstract class CalFather
    {
        public double NumberOne
        {
            get;
            set;
        }
        public double NumberTwo
        {
            get;
            set;
        }

        public CalFather(double One,double Two)
        {
            this.NumberOne = One;
            this.NumberTwo = Two;
        }

        public abstract double GetResult();
    }
    /// <summary>
    /// 加法的子類
    /// </summary>
    public class Add:CalFather
    {
        public Add(double one,double two):base(one,two)
        {

        }
        public override double GetResult()
        {
            return this.NumberOne + this.NumberTwo;
        }
    }
    /// <summary>
    /// 減法的子類
    /// </summary>
    public class Sub:CalFather
    {
        public Sub(double one,double two):base(one,two)
        {

        }
        public override double GetResult()
        {
            return this.NumberOne - this.NumberTwo;
        }
    }
    /// <summary>
    /// 乘法的子類
    /// </summary>
    public class Ride:CalFather
    {
        public Ride(double one,double two):base(one,two)
        {

        }
        public override double GetResult()
        {
            return this.NumberOne * this.NumberTwo;
        }
    }
    /// <summary>
    /// 除法的子類
    /// </summary>
    public class Chu:CalFather
    {
        public Chu(double one,double two):base(one,two)
        {

        }
        public override double GetResult()
        {
            return this.NumberOne / this.NumberTwo;
        }
    }
}


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI