• c#中简单工厂模式


    运算类
    
     public  class yunsuan
        {
           public static operation create(string operate) {
               operation oper = null;
               switch (operate) { 
                       case "+":
                       oper = new add();
                       break;
                   case "-":
                       oper = new sub();
                       break;
                   case "*":
                       oper = new cheng();
                       break;
                   case "/":
                       oper = new chu();
                       break;
               
               
               }
               return oper;
           
           }
        }
        //类的属性
        public class operation
        {
            private double numberA = 0;
            private double numberB = 0;
    
            public double NumberA {
                get { return numberA; }
                set { numberA = value; }
            
            }
            public double NumberB {
                get { return numberB; }
                set { numberB = value; }
            }
            public virtual double getresult() {
                double result = 0;
                return result;
               
            }
    
        }
        //类继承运算
    
        class add : operation {//加法
            public override double getresult()
            {
                double result = 0;
                result = NumberA + NumberB;
                return result;
            }
        }
        //减法
        class sub : operation {
            public override double getresult()
            {
                double result = 0;
                result = NumberA - NumberB;
                return result;
            }
        }
        //乘法
        class cheng : operation {
            public override double getresult()
            {
                double result = 0;
                result = NumberA * NumberB;
                return result;
            }
        
        }
        class chu : operation {
            public override double getresult()
            {
                double result = 0;
                if (NumberB == 0) {
                    Console.WriteLine("被除数不能为0");
                }
                result = NumberA / NumberB;
                return result;
            }
        }

    客户端代码

      //简单工厂模式 
                operation oper;
                oper = yunsuan.create("/");
                oper.NumberA = 10;
                oper.NumberB = 10;
                double result = oper.getresult();
                Console.WriteLine(result);
                Console.ReadKey();
  • 相关阅读:
    深圳成为全球第一个100%电动公共汽车的城市
    layui 数据表格按钮事件绑定和渲染
    Layui 改变数据表格样式覆盖
    js 遍历删除数组
    layui 数据表格最简单的点击事件
    layui 数据表格使用
    Layui 解决动态图标不动的问题
    Js 改变时间格式输出格式
    PHP 面向对象的数据库操作
    PHP SQL预处理
  • 原文地址:https://www.cnblogs.com/mengluo/p/4825184.html
Copyright © 2020-2023  润新知