ASP.NET四则运算--策略模式
在ASP.NET中实现四则运算,同样使用了类的封装,以及策略模式。
只不过是把封装的类、前台代码以及后台的代码分离开来,但同样是要达到功能的实现。
Calculator.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 /// <summary> 7 ///Calculator 的摘要说明 8 /// </summary> 9 public abstract class Calculator 10 { 11 public abstract double Cal(double a, double b); 12 } 13 public class Add : Calculator //派生类Add继承抽象类Calculator 14 { 15 public override double Cal(double a, double b)//并重写了抽象方法Cal 16 { 17 double result = 0; 18 result = a + b; 19 return result; 20 } 21 } 22 public class Sub : Calculator 23 { 24 public override double Cal(double a, double b) 25 { 26 double result = 0; 27 result = a - b; 28 return result; 29 } 30 } 31 public class Mul : Calculator 32 { 33 public override double Cal(double a, double b) 34 { 35 double result = 0; 36 result = a * b; 37 return result; 38 } 39 } 40 public class Div : Calculator 41 { 42 public override double Cal(double a, double b) 43 { 44 double result = 0; 45 result = a / b; 46 return result; 47 } 48 } 49 public class Context //上下文 50 { 51 private Calculator calculate = null;//实例化一个基类的引用对象 52 public Context(Calculator _cal)//_cal为派生类的一个对象 53 { 54 this.calculate = _cal; //把派生类的对象赋给基类的引用对象 55 } 56 public double Cal(double a, double b, String symbol) 57 { 58 return this.calculate.Cal(a, b);//返回计算结果 59 } 60 }
index.aspx.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 public partial class index : System.Web.UI.Page 9 { 10 protected void Page_Load(object sender, EventArgs e) 11 { 12 //界面加载 13 } 14 protected void Cal_Click(object sender, EventArgs e) 15 { 16 string symbol = DropDownList1.SelectedItem.ToString(); 17 double a = Convert.ToDouble(TextBox1.Text); 18 double b = Convert.ToDouble(TextBox2.Text); 19 Context contex = null; 20 if (DropDownList1.SelectedIndex == 1) 21 { 22 contex = new Context(new Add()); //加法策略 23 } 24 else if (DropDownList1.SelectedIndex == 2) 25 { 26 contex = new Context(new Sub()); //减法策略 27 } 28 else if (DropDownList1.SelectedIndex == 3) //若为乘号 29 { 30 contex = new Context(new Mul()); //乘法策略 31 } 32 else if (DropDownList1.SelectedIndex == 4) //若为乘号 33 { 34 contex = new Context(new Div()); //除法策略 35 } 36 string answer = contex.Cal(a, b, symbol).ToString(); //用answer来存计算出来的答案,此时已经计算出a,b两个数的运算结果。 37 38 string result = TextBox1.Text + DropDownList1.SelectedItem.ToString() + TextBox2.Text;//把运算式子存在result里面 39 if (TextBox3.Text == answer) //如果输入答案与计算出的answer相等 40 { 41 Response.Write("<script>alert('回答正确!')</script>"); //弹出回答正确 42 ListBox1.Items.Add(result + "=" + TextBox3.Text.Trim() + "√");//并把运算式子存在listbox里 43 } 44 45 else //如果答错 46 { 47 Response.Write("<script>alert('答题错误!')</script>"); //弹出答题错误 48 ListBox1.Items.Add(result + "=" + TextBox3.Text.Trim() + "×");//同样把运算式子放在listbox 49 } 50 TextBox1.Text = "";//把文本框清空,进行下一次出题 51 TextBox2.Text = ""; 52 TextBox3.Text = ""; 53 } 54 }
运行测试:
回答正确给出提示!
出题保存到listbox列表:
ASP.NET中,把Calculator.cs放到App_Code文件夹。
总的来说不算难,主要就是初步实现对类的封装,以及策略模式的使用!感受一下:经过老师给我们介绍的设计模式,先是让我们欣赏,发现代码原来可以写的这么漂亮!