重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要为大家展示了“Asp.Net中继承和多态性的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Asp.Net中继承和多态性的示例分析”这篇文章吧。
创新互联主要从事成都网站建设、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务岳阳,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
页面文件TestProfileClass2.aspx
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" Src="profile2.cs" %> <%@ Import Namespace="shai" %>简单的继承/多态例子
object output:
类定义文件profile2.cs using System; using System.IO; using System.Xml; namespace shai //名称空间shai { interface ISaveData //定义一个接口ISaveData,他作为Profile的基类被继承,由于其有多态性的特点,可以被 { //不同类调用其中的方法,并修改 void save(); } public class Profile:ISaveData //定义类Profile { protected string _firstname; //里面包含三个属性,分别为_firstname、_lastname、phonenumber protected string _lastname; protected string _phonenumber; public Profile() //为属性定义其中的初始值 { _firstname = "Saidy"; _lastname = "Chen"; _phonenumber = "(010)88716990"; } public string getPhonenumber() //Profile类中的方法getPhonenumber() { return _phonenumber; } public virtual void setPhonenumber(string phonenumber) { _phonenumber = phonenumber; } public string getFirstname() //Profile类中的方法getFirstname() { return _firstname; } public void setFirstname(string firstname) { _firstname = firstname; } public string getLastname() //Profile类中的方法getLastname() { return _lastname; } public void setLastname(string lastname) { _lastname = lastname; } public virtual void save() //调用ISaveData接口的save()方法 { //保存数据为Text格式 FileStream fs = new FileStream("D:\\myweb2\\profile2.txt",FileMode.Create,FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("Firstname:"+_firstname.ToString()); sw.WriteLine("Lastname:"+_lastname.ToString()); sw.WriteLine("Phone:"+_phonenumber.ToString()); sw.Flush(); sw.Close(); fs.Close(); } } public class ExtendedProfile:Profile 建立Profile子类ExtendedProfile,他可以继承Profile中的方法 { protected string _address1; //子类ExtendedProfile的属性 protected string _address2; protected string _city; protected string _state; protected string _postal; protected string _description; public ExtendedProfile() //子类ExtendedProfile中属性的初始值 { _address1 = "清华大学"; _address2 = "清华大学物理实验室"; _city = "北京"; _state = "北京"; _postal = "100024"; _description = "教授"; } public override void setPhonenumber(string phonenumber) //继承类Profile中的setPhonenumber()方法 { //setPhonenumber()方法的重载 _phonenumber = phonenumber; } public string getAddress1() //子类ExtendedProfile中的方法getAddress1(),以下类推 { return _address1; } public string getAddress2() { return _address2; } public void setAddress(string address1,string address2) { _address1 = address1; _address2 = address2; } public string getCity() { return _city; } public void setCity(string city) { _city = city; } public string getState() { return _state; } public void setState(string state) { _state = state; } public string getPostal() { return _postal; } public void setPostal(string postal) { _postal = postal; } public string getDescription() { return _description; } public void setDescription(string description) { _description = description; } public override void save() //调用接口ISaveData()中的方法save(),save()方法的重载,由于多态性 { //子类ExtendedProfile可以自定义并修改save()方法 string _document = "d:\\myweb2\\saidy.xml"; XmlTextWriter writer = null; //保存为一个Xml文件 try { writer = new XmlTextWriter(_document,null); writer.Formatting = Formatting.Indented; writer.WriteStartDocument(false); writer.WriteDocType("Profile",null,null,null); //表示 writer.WriteStartElement("Profile"); //生成根元素 writer.WriteElementString("firstname",_firstname); //生成子元素_firstname writer.WriteElementString("lastname",_lastname); writer.WriteElementString("phonenumber",_phonenumber); writer.WriteElementString("address1",_address1); writer.WriteElementString("address2",_address2); writer.WriteElementString("city",_city); writer.WriteElementString("state",_state); writer.WriteElementString("postal",_postal); writer.WriteEndElement(); writer.Flush(); writer.Close(); } catch(Exception ee) { Console.WriteLine("Exception:{0}",ee.ToString()); } } } }
以上是“Asp.Net中继承和多态性的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!