zoukankan      html  css  js  c++  java
  • 步步为营-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 运行效果

  • 相关阅读:
    redis 1 简单介绍和存储的数据结构
    mysql 14 覆盖索引+回表
    mysql 13 B+tree中存储数据的格式 页
    java Arrays.asList() 数组转集合
    java 迭代器
    mysql 12 SQL优化策略
    mysql 11 执行计划
    mysql 10 索引面试题分享
    搭建一个开源项目2-打造另一个环境以及解决上期问题
    搭建一个开源项目1-如何搭建Linux虚拟机
  • 原文地址:https://www.cnblogs.com/YK2012/p/6708403.html
Copyright © 2011-2022 走看看