コード例 #1
0
ファイル: OperationFactory.cs プロジェクト: paulRoux/Language
        public static Operation CreateOperation(string strOperator)
        {
            Operation opr = null;

            switch (strOperator)
            {
            case "+":
                opr = new OperationAdd();
                break;

            case "-":
                opr = new OperationSub();
                break;

            case "*":
                opr = new OperationMul();
                break;

            case "/":
                opr = new OperationDiv();
                break;

            default:
                throw new Exception("运算符输入错误!");
            }
            return(opr);
        }
コード例 #2
0
        /// <summary>
        /// 只需输入运算符,工厂就能实例化出合适的对象,通过多态,返回父类的方式实现了计算器的结果
        /// </summary>
        /// <param name="operate">运算符</param>
        /// <returns></returns>
        public static Operation createOpeate(EnumOperator op)
        {
            Operation oper = null;

            switch (op)
            {
            case EnumOperator.Add:
                oper = new OperationAdd();
                break;

            case EnumOperator.Minus:
                oper = new OperationSub();
                break;

            case EnumOperator.Multiply:
                oper = new OperationMul();
                break;

            case EnumOperator.Divide:
                oper = new OperationDiv();
                break;

            default:
                break;
            }
            return(oper);
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: mengguoru/C_sharp_practices
 public static Operation createOperation(string item)
 {
     Operation temp = null;
     switch(item)
     {
         case "+":
             temp = new OperationAdd();
             break;
         case "-":
             temp = new OperationSub();
             break;
         case "*":
             temp = new OperationMul();
             break;
         case "/":
             temp = new OperationDiv();
             break;
     }
     return temp;
 }
コード例 #4
0
        public static Operation createOperate(string operate)
        {
            Operation oper = null;

            switch (operate)
            {
            case "+":
                oper = new OperationAdd();
                break;

            case "-":
                oper = new OperationSub();
                break;

            case "*":
                oper = new OperationMul();
                break;

            case "/":
                oper = new OperationDiv();
                break;
            }
            return(oper);
        }
コード例 #5
0
        public static Operation createOperation(string item)
        {
            Operation temp = null;

            switch (item)
            {
            case "+":
                temp = new OperationAdd();
                break;

            case "-":
                temp = new OperationSub();
                break;

            case "*":
                temp = new OperationMul();
                break;

            case "/":
                temp = new OperationDiv();
                break;
            }
            return(temp);
        }