zoukankan      html  css  js  c++  java
  • 策略模式

    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 = "";
    }
    }

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

  • 相关阅读:
    python基础之集合
    python中关于赋值,深浅拷贝
    python小数据池
    python关于解构的例子
    test
    python字典的增,删,改,查
    python中元组常识,以及for 与 range 的用法!
    protobuf.js在长连接中前后台的交互使用方式
    vue-cli的浏览器兼容babel的配置
    前端处理在web打开app功能,有app就打开,没app就提示需要下载直接跳到下载app的页面
  • 原文地址:https://www.cnblogs.com/zj15517225953/p/5023903.html
Copyright © 2011-2022 走看看