在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
平时在asp.net开发中,很少使用到抽象类,一个小小例子来学习一下。 抽象类(abstract class),不能实例化,因此我们可以把不被实例化的类,宣告为抽象类。 抽象类内包含了抽象方法(abstract void),它只能写在抽象类中,没有实现部分,只能在派生类重写(override)或实现。 去年小新房装修,去了家居装饰城,看了各式各样的灯饰(实例),统称为灯(抽象)。下面的在抽象类Light中,有两个抽象的方法。
Light
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for Light /// </summary> namespace Insus.NET { public abstract class Light { public Light() { // // TODO: Add constructor logic here // } public abstract void TurnOn(); public abstract void TurnOff(); } }
IncandescentLamp
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for IncandescentLamp /// </summary> namespace Insus.NET { public class IncandescentLamp : Light { public IncandescentLamp() : base() { // // TODO: Add constructor logic here // } public override void TurnOn() { HttpContext.Current.Response.Clear(); string On_Light = "<img src='Images/Light_O.gif' />"; HttpContext.Current.Response.Write(On_Light); } public override void TurnOff() { HttpContext.Current.Response.Clear(); string Off_Light = "<img src='Images/Light_C.gif' />"; HttpContext.Current.Response.Write(Off_Light); } } }
View Code
<asp:CheckBox ID="CheckBoxSwitch" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBoxSwitch_CheckedChanged" />
View Code
IncandescentLamp Il1 = new IncandescentLamp();
View Code
protected void Page_Load(object sender, EventArgs e) { Il1.TurnOff(); }
View Code
protected void CheckBoxSwitch_CheckedChanged(object sender, EventArgs e) { var cb = (CheckBox)sender; if (cb.Checked) Il1.TurnOn(); }
完整代码:
|
请发表评论