1) 从网页原代码里获取所需的值:ide
例:spa
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) 建立基类:将获取的值封装,并添加方法。code
例:blog
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 建立逻辑功能类,好比两数相乘继承
例:ci
namespace gongcheng { public class zhengchang:jisuan //继承基类 { public override decimal getresult() //重写方法 { return a * b; } }
4 建立简单工厂类:并传值get
例:string
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; //最后将调取的类返回出去
}io
}
}class
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(); } } }
界面以下: