网站建设资讯

NEWS

网站建设资讯

C#中的简单工厂设计模式示例-创新互联

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

成都网络公司-成都网站建设公司成都创新互联十年经验成就非凡,专业从事网站设计制作、网站制作,成都网页设计,成都网页制作,软文营销一元广告等。十年来已成功提供全面的成都网站建设方案,打造行业特色的成都网站建设案例,建站热线:18980820575,我们期待您的来电!

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;
       }
   }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:C#中的简单工厂设计模式示例-创新互联
转载来源:http://cdweb.net/article/djdgdh.html