1) 从网页原代码里获取所需的值:
例:
private void Button1_Click(object sender, EventArgs e) { string a = textBox1.Text; string b = textBox2.Text; string c = comboBox1.Text; celue cl = new celue(a, b, c); //将策略类实例化并传值 label5.Text = Convert.ToString(cl.result());//调取策略类的方法 }
2) 创建基类:将获取的值封装,并添加方法。
例:
namespace gongcheng { public class jisuan //将获取的值封装 { private decimal _a; public decimal a { get { return _a; } set { _a = value; } } private decimal _b; public decimal b { get { return _b; } set { _b = value; } } public virtual decimal getresult() //创建虚拟方法 { decimal c = 0; return c; } }
3 创建逻辑功能类,比如两数相乘
例:
namespace gongcheng { public class zhengchang:jisuan //继承基类 { public override decimal getresult() //重写方法 { return a * b; } }
4 创建简单工厂类:并传值
例:
namespace gongcheng { public class factiory { public static jisuan creatclass(string c) //将获取的下拉列表里的值传进来 { jisuan j = null; if (c == "正常") //如果下拉列表框的值为正常 { j = new zhengchang(); //那么调用zhengchang方法类里的方法 } if ( c == "满300减100") // 如果下拉列表框的值为满300减100 { j = new manjian(); //那么调用manjian方法类里的方法 }
               return j;             //最后将调取的类返回出去
             }
         }
   }
5 创建策略类 :
例:
namespace gongcheng
{
    public class celue
    {
        private jisuan js;     //基类封装
        public celue(string a, string b, string c) //写个方法并传值
        {
            js = factiory.creatclass(c); //将工厂类方法实例化传值
            js.a = Convert.ToDecimal(a); 
            js.b = Convert.ToDecimal(b);
        }
        public decimal result()  //调用结果
        {
            return js.getresult();
        }
    }
}
界面如下: