网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C#中的简单工厂设计模式示例

这个是用面向对象的方法来实现加,减,乘,除的计算,使用了“简单工厂的设计模式”。

创新互联专注于曲沃网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供曲沃营销型网站建设,曲沃网站制作、曲沃网页设计、曲沃网站官网定制、重庆小程序开发公司服务,打造曲沃网络公司原创品牌,更为您提供曲沃网站排名全网营销落地服务。

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

namespace 简单公司实现计算1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入第一个数字:");
          double n1=  Convert.ToDouble( Console.ReadLine());
          Console.WriteLine("请输入第二个数字:");
         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();

        }

        ///


        /// 简单工厂模式
        ///

        /// 传入的操作符
        /// 第一个运算的数字
        /// 第二个运算的数字
        ///
        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;
        }
    }
    ///


    /// 父类模型,用abstract抽象函数来实现多态
    ///

    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();
    }
    ///


    /// 加法的子类
    ///

    public class Add:CalFather
    {
        public Add(double one,double two):base(one,two)
        {

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


    /// 减法的子类
    ///

    public class Sub:CalFather
    {
        public Sub(double one,double two):base(one,two)
        {

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


    /// 乘法的子类
    ///

    public class Ride:CalFather
    {
        public Ride(double one,double two):base(one,two)
        {

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


    /// 除法的子类
    ///

    public class Chu:CalFather
    {
        public Chu(double one,double two):base(one,two)
        {

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


文章标题:C#中的简单工厂设计模式示例
浏览地址:http://bjjierui.cn/article/isgeej.html

其他资讯