• 策略模式


    fengzhuang.cs

    复制代码
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
    
      public abstract class fengzhuang
     {
         public abstract double Cal(double a, double b);
     }
     public class Add : fengzhuang      
     {
         public override double Cal(double a, double b)
         {
             double result = 0;
             result = a + b;
             return result;
         }
     }
     public class Sub : fengzhuang
     {
         public override double Cal(double a, double b)
         {
             double result = 0;
             result = a - b;
             return result;
         }
     }
     public class Mul :fengzhuang
     {
         public override double Cal(double a, double b)
         {
             double result = 0;
             result = a * b;
             return result;
    } } public class Div : fengzhuang { public override double Cal(double a, double b) { double result = 0; result = a / b; return result; } } public class Context { private fengzhuang calculate = null; public Context(Calculator _cal) { this.calculate = _cal; } public double Cal(double a, double b, String symbol) { return this.calculate.Cal(a, b); } }

    代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class index : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Cal_Click(object sender, EventArgs e)
    {
    string symbol = DropDownList1.SelectedItem.ToString();
    double a = Convert.ToDouble(TextBox1.Text);
    double b = Convert.ToDouble(TextBox2.Text);
    Context contex = null;
    if (DropDownList1.SelectedIndex == 1)
    {
    contex = new Context(new Add()); 
    }
    else if (DropDownList1.SelectedIndex == 2)
    {
    contex = new Context(new Sub()); 
    }
    else if (DropDownList1.SelectedIndex == 3) 
    {
    contex = new Context(new Mul()); 
    }
    else if (DropDownList1.SelectedIndex == 4) 
    {
    contex = new Context(new Div()); 
    }
    string answer = contex.Cal(a, b, symbol).ToString();

    string result = TextBox1.Text + DropDownList1.SelectedItem.ToString() + TextBox2.Text;
    if (TextBox3.Text == answer)
    {
    Response.Write("<script>alert('回答正确!')</script>");
    ListBox1.Items.Add(result + "=" + TextBox3.Text.Trim());
    }

    else 
    {
    Response.Write("<script>alert('答题错误!')</script>");
    ListBox1.Items.Add(result + "=" + TextBox3.Text.Trim() );
    }
    TextBox1.Text = "";
    TextBox2.Text = "";
    TextBox3.Text = "";
    }
    }

    总结:看来想做成功还得伙伴。要想走得快一个人走,但要想走得远还得伙伴挽着手一起走。有人帮忙就是好

  • 相关阅读:
    LeetCode 224. 基本计算器 栈 双指针
    LeetCode 150. 逆波兰表达式求值 栈
    LeetCode 387. 字符串中的第一个唯一字符 哈希
    LeetCode 316. 去除重复字母 栈 哈希
    LeetCode 44. 通配符匹配 dp
    禁止屏幕旋转并同时解决以至于导致Activity重启的方法
    让振动器振动起来——Vibrator的使用
    简单的JDBC封装
    js jquery ajax 清除ie下的缓存问题
    angular.js 下拉框选中 根据后台返回值
  • 原文地址:https://www.cnblogs.com/zj15517225953/p/5023903.html
Copyright © 2020-2023  润新知