• 二.简单工厂


    简单工厂:

           简单工厂处理的是如何选择我们override的虚方法。

    public virtualdouble GetResult()

            {

                double result=0;

                return result;

            }

    不一定是虚方法,抽象方法也可以:

    abstract  classCashSuper

        {

            public abstract doubleacceptCash(double money);

    }

    父类,虚方法和子类,具体方法 —— 为第一篇:虚方法的代码

    调用工厂:

    class Program

        {

           static void Main(string[] args)

           {

               try

               {

                    Operate op=new Operate ();

                    Console.WriteLine("输入运算符");

                    //此处OP重新赋值,属性值也重新赋值

                    op= Factory.SetOperate(Console.ReadLine());

                    Console.WriteLine("输入数字1");

                    op.Num1 =Convert.ToDouble(Console.ReadLine());

                    Console.WriteLine("输入数字2");

                    op.Num2 = Convert.ToDouble(Console.ReadLine());

                   Console.WriteLine(op.GetResult());

               }

               catch (Exception ex)

               {

                    Console.WriteLine("错误:"+ex.Message );

               }

               Console.ReadKey();

            }

    }

    工厂类:

       class Factory

        {

           public static Operate SetOperate(string operate)

           {

               Operate oper = null;

               switch (operate)

               {

                    case "+":

                        oper=new OperateAdd();

                       break;

                    。。。。。。

               }

               return oper;

           }

        }

  • 相关阅读:
    SPOJ SAMER08A
    SPOJ TRAFFICN
    CS Academy Set Subtraction
    CS Academy Bad Triplet
    CF Round 432 C. Five Dimensional Points
    CF Round 432 B. Arpa and an exam about geometry
    SPOJ INVCNT
    CS Academy Palindromic Tree
    身体训练
    简单瞎搞题
  • 原文地址:https://www.cnblogs.com/yaoge/p/1815220.html
Copyright © 2020-2023  润新知