计算器实例
(利用面向对象封装的特性,将业务的逻辑与界面的逻辑分开,降低耦合度)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Net; 7 using System.Net.Sockets; 8 9 10 namespace Test 11 { 12 //运算类() 13 public class Operation 14 { 15 public static double GetResult(double numberA, double numberB, string operate) 16 { 17 double result = 0d; 18 switch (operate) 19 { 20 case "+": 21 result = numberA + numberB; 22 break; 23 case "-": 24 result = numberA - numberB; 25 break; 26 case "*": 27 result = numberA * numberB; 28 break; 29 case "/": 30 result = numberA / numberB; 31 break; 32 default: 33 break; 34 } 35 return result; 36 } 37 } 38 39 public class Program 40 { 41 static void Main(string[] args) 42 { 43 try 44 { 45 Console.Write("请输入数字A:"); 46 string strNumberA = Console.ReadLine(); 47 Console.Write("请选择运算符号(+ 、- 、*、/、):"); 48 string strOperate = Console.ReadLine(); 49 Console.Write("请输入数字B:"); 50 string strNumberB = Console.ReadLine(); 51 string strResult = ""; 52 strResult = Convert.ToString(Operation.GetResult(Convert.ToDouble(strNumberA), 53 Convert.ToDouble(strNumberB), strOperate)); 54 Console.WriteLine("结果是:" + strResult); 55 Console.ReadLine(); 56 } 57 catch (Exception ex) 58 { 59 Console.WriteLine("您输入的有错:" + ex.Message); 60 } 61 } 62 } 63 }
///////////////////////紧耦合 vs 松耦合
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Net.Sockets; namespace Test { //运算类 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 OperationAdd:Operation { public override double GetResult() { double result = 0; result = NumberA + NumberB; return result; } } //减法 class OperationSub : Operation { public override double GetResult() { double result = 0; result = NumberA - NumberB; return result; } } //乘法 class OperationMul : Operation { public override double GetResult() { double result = 0; result = NumberA * NumberB; return result; } } //除法 class OperationDiv : Operation { public override double GetResult() { double result = 0; if (NumberB == 0) throw new Exception("除数不能为0"); result = NumberA / NumberB; return result; } } //简单工厂 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; } return oper; } } public class Program { static void Main(string[] args) { Console.Write("请选择运算符号(+ 、- 、*、/、):"); string strOperate = Console.ReadLine(); Operation oper; oper = OperationFactory.createOperate(strOperate); Console.Write("请输入数字A:"); oper.NumberA = Convert.ToDouble(Console.ReadLine()); Console.Write("请输入数字B:"); oper.NumberB = Convert.ToDouble(Console.ReadLine()); double result = oper.GetResult(); Console.WriteLine("结果是:" + result); Console.ReadLine(); } } }