• 步步为营-09-简单工厂类-计算器


    先把整体UI建好

    1 分析需要个计算的基类,一个构造函数,两个属性,一个方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Calcultor
    {
       public abstract class Calculate
        {
            public Calculate(double a,double b) {
                this.FirstNumber = a;
                this.SecondNumber = b;
            }
            public double FirstNumber { get; set; }
            public double SecondNumber { get; set; }
    
            public abstract double Result();
        }
    }
    Calculate

    2 定义子类+-*/

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Calcultor
    {
        class A:Calculate
        {
             public A(double  a, double b) : base(a,b)
            {
                this.FirstNumber = a;
                this.SecondNumber = b;
    
    
            }
            public override double Result()
            {
                return this.FirstNumber + this.SecondNumber;
            }
        }
    }
    +
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Calcultor
    {
        class S:Calculate
        {
             public S(double  a, double b) : base(a,b)
            {
                this.FirstNumber = a;
                this.SecondNumber = b;
            }
            public override double Result()
            {
                return this.FirstNumber - this.SecondNumber;
            }
        }
    }
    -
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Calcultor
    {
        class M:Calculate
        {
             public M(double  a, double b) : base(a,b)
            {
                this.FirstNumber = a;
                this.SecondNumber = b;
            }
            public override double Result( )
            {
                return this.FirstNumber * this.SecondNumber;
            }
        }
    }
    *
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Calcultor
    {
        class D:Calculate
        {
             public D(double  a, double b) : base(a,b)
            {
                this.FirstNumber = a;
                this.SecondNumber = b;
            }
            public override double Result( )
            {
                if (this.SecondNumber != 0)
                {
                    return this.FirstNumber / this.SecondNumber;
                }
                return 0;
            }
        }
    }
    /

    3 定义抽象工厂

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Calcultor
    {
       public  class Factory
        {
           public static Calculate GetResult(string oper,double a,double b)
           {
               Calculate ca = null;
               switch (oper)
               {
                   case "+":
                       ca = new A(a,b);
                       break;
                   case "-":
                       ca = new S(a, b);
                       break;
                   case "*":
                       ca = new M(a, b);
                       break;
                   case "/":
                       ca = new D(a, b);
                       break;
               }
               return ca;
           }
    
        }
    }
    Factory

    4 Main方法

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Calcultor
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnAdd_Click(object sender, EventArgs e)
            {
                string op = btnAdd.Text;
                GetResult(op);
            }
    
         
    
            private void btnSub_Click(object sender, EventArgs e)
            {
                string op = btnSub.Text;
                GetResult(op);
            }
    
            private void btnMul_Click(object sender, EventArgs e)
            {
                string op = btnMul.Text;
                GetResult(op);
            }
    
            private void btnDiv_Click(object sender, EventArgs e)
            {
                string op = btnDiv.Text;
                GetResult(op);
            }
            private void GetResult(string op)
            {
                double a = Convert.ToDouble(txtFirst.Text);
                double b = Convert.ToDouble(txtSecond.Text);
                Calculate c = Factory.GetResult(op, a, b);
                lblResult.Text = c.Result().ToString();
            }
        }
    }
    Main

    5 运行效果

  • 相关阅读:
    show()的几种方法
    sql的交叉连接,内连接,左外连接,右外连接,全外连接总结
    MySQL数据库引擎介绍、区别、创建和性能测试的深入分析
    RedHat9通过Host-only配置网络连接
    解决Struts2.2.20版本的标签不支持style属性的问题
    SQL查询数据库中所有含有某一列的所有表
    简单工厂模式
    vc常用类总结(转载)
    嵌入式程序员应知道的0x10个C语言Tips
    C语言位操作
  • 原文地址:https://www.cnblogs.com/YK2012/p/6708403.html
Copyright © 2020-2023  润新知