一、面向对象的好处:可维护,可复用,可扩展,灵活性好。
二、具体事例:计算器
1、类结构图:
2、实例代码:
Operation运算类(OperationLibrary.Operation.cs)
using System; using System.Collections.Generic; using System.Text; namespace OperationLibrary { /// <summary> /// 运算类 /// </summary> public class Operation { private double _numberA = 0; private double _numberB = 0; /// <summary> /// 数字A /// </summary> public double NumberA { get { return _numberA; } set { _numberA = value; } } /// <summary> /// 数字B /// </summary> public double NumberB { get { return _numberB; } set { _numberB = value; } } /// <summary> /// 得到运算结果 /// </summary> /// <returns></returns> public virtual double GetResult() { double result = 0; return result; } /// <summary> /// 检查输入的字符串是否准确 /// </summary> /// <param name="currentNumber"></param> /// <param name="inputString"></param> /// <returns></returns> public static string checkNumberInput(string currentNumber, string inputString) { string result = ""; if (inputString == ".") { if (currentNumber.IndexOf(".") < 0) { if (currentNumber.Length == 0) result = "0" + inputString; else result = currentNumber + inputString; } } else if (currentNumber == "0") { result = inputString; } else { result = currentNumber + inputString; } return result; } } /// <summary> /// 加法类 /// </summary> class OperationAdd : Operation { public override double GetResult() { double result = 0; result = NumberA + NumberB; return result; } } /// <summary> /// 减法类 /// </summary> class OperationSub : Operation { public override double GetResult() { double result = 0; result = NumberA - NumberB; return result; } } /// <summary> /// 乘法类 /// </summary> class OperationMul : Operation { public override double GetResult() { double result = 0; result = NumberA * NumberB; return result; } } /// <summary> /// 除法类 /// </summary> class OperationDiv : Operation { public override double GetResult() { double result = 0; if (NumberB == 0) throw new Exception("除数不能为0。"); result = NumberA / NumberB; return result; } } /// <summary> /// 平方类 /// </summary> class OperationSqr : Operation { public override double GetResult() { double result = 0; result = NumberB * NumberB; return result; } } /// <summary> /// 平方根类 /// </summary> class OperationSqrt : Operation { public override double GetResult() { double result = 0; if (NumberB < 0) throw new Exception("负数不能开平方根。"); result = Math.Sqrt(NumberB); return result; } } /// <summary> /// 相反数类 /// </summary> class OperationReverse : Operation { public override double GetResult() { double result = 0; result = -NumberB; return result; } } /// <summary> /// 运算类工厂 /// </summary> public class OperationFactory { 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; } case "sqr": { oper = new OperationSqr(); break; } case "sqrt": { oper = new OperationSqrt(); break; } case "+/-": { oper = new OperationReverse(); break; } } return oper; } } }
计算器控制台(Program.cs)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Simple_Factory; namespace 计算器控制台 { class Program { static void Main(string[] args) { try { Operation ope; Console.WriteLine("请输入第一个操作数"); string _strA = Console.ReadLine(); Console.WriteLine("请输入操作符"); string _operate = Console.ReadLine(); Console.WriteLine("请输入第二个操作数"); string _strB = Console.ReadLine(); ope = OperationFactory.createOperate(_operate); ope.NumberA = Convert.ToDouble(_strA); ope.NumberB = Convert.ToDouble(_strB); double result = ope.GetResault(); Console.WriteLine("运算结果是:" + result.ToString()); } catch (Exception exp) { Console.WriteLine("您输入的结果有误:" + exp.Message); } } } }
计算器窗体(Form1.cs)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using OperationLibrary; namespace 计算器 { /// <summary> /// 面向对象编程 /// 1、加运算类 /// 2、加运算子类 /// 3、加运算类工厂 /// 4、更改Form1 public partial class Form1 : Form { bool bOperate = false; Operation oper; public Form1() { InitializeComponent(); } private void button0_Click(object sender, EventArgs e) { if (bOperate) { txtShow.Text = ""; bOperate = false; } string number = ((Button)sender).Text; txtShow.Text = Operation.checkNumberInput(txtShow.Text, number); } private void buttonClear_Click(object sender, EventArgs e) { txtShow.Text = ""; } private void buttonAdd_Click(object sender, EventArgs e) { if (txtShow.Text != "") { oper = OperationFactory.createOperate(((Button)sender).Text); oper.NumberA = Convert.ToDouble(txtShow.Text); bOperate = true; } } private void buttonEqual_Click(object sender, EventArgs e) { if (txtShow.Text != "") { if (((Button)sender).Text != "=") { oper = OperationFactory.createOperate(((Button)sender).Text); } oper.NumberB = Convert.ToDouble(txtShow.Text); txtShow.Text = oper.GetResult().ToString(); bOperate = true; } } private void Form1_Load(object sender, EventArgs e) { } } }
3、Form1设计视图
4、如果我们某一天要修改加法运算,只需要修改OperationAdd。如果我们要增加或删除某些运算只需要增加或删除相应的子类和工厂类里的switch分支就好了。