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(); } } }
界面以下: