首页 > 代码库 > 步步为营-09-简单工厂类-计算器
步步为营-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(); } }
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; } } }
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(); } } }
5 运行效果
步步为营-09-简单工厂类-计算器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。