以下为引用的内容: TestProfileClass2.aspx 页面文件 <%@PageLanguage="C#"ContentType="text/html"ResponseEncoding="gb2312"Src="profile2.cs"%> <%@ImportNamespace="shai"%> <html> <head> <style> div{font:Arial,Helvetica,sans-serif; background-color:#cccccc; border-color:black; border-width:1; border-style:solid; padding:10,10,10,10; } </style> <scriptlanguage="c#"runat="server"> publicvoidPage_Load(Objectsender,EventArgse) { Profileprofile=newProfile(); message.InnerHtml+="<u>ProfileClass</u><br>"; message.InnerHtml+="Firstname:"+profile.getFirstname()+"<br>"; message.InnerHtml+="Lastname:"+profile.getLastname()+"<br>"; message.InnerHtml+="Phone:"+profile.getPhonenumber()+"<br><br>";
profile.save();
ExtendedProfileextendedprofile=newExtendedProfile(); message.InnerHtml+="<u>ExtendedProfileClass</u></br>"; message.InnerHtml+="Firstname:"+profile.getFirstname()+"<br>"; message.InnerHtml+="Lastname:"+profile.getLastname()+"<br>"; message.InnerHtml+="Phone:"+extendedprofile.getPhonenumber()+"<br>"; message.InnerHtml+="Address1:"+extendedprofile.getAddress1()+"<br>"; message.InnerHtml+="Address2:"+extendedprofile.getAddress2()+"<br>"; message.InnerHtml+="City:"+extendedprofile.getCity()+"<br>"; message.InnerHtml+="State:"+extendedprofile.getState()+"<br>"; message.InnerHtml+="Postal:"+extendedprofile.getPostal()+"<br>"; message.InnerHtml+="Description:"+extendedprofile.getDescription()+"<br>";
extendedprofile.save(); } </script> <metahttp-equiv="Content-Type"content="text/html;charset=gb2312"> <title></title> </head> <bodystyle="font:Arial,Helvetica,sans-serif"> <b><h3>简单的继承、多态例子</h3></b><br><br> objectoutput:<br> <br> <divid="message"runat="server"/> </body> </html>
profile2.cs 类定义文件 usingSystem; usingSystem.IO; usingSystem.Xml; namespaceshai//名称空间shai { interfaceISaveData//定义一个接口ISaveData,他作为Profile的基类被继承,由于其有多态性的特点,可以被 {//不同类调用其中的方法,并修改 voidsave(); }
publicclassProfile:ISaveData//定义类Profile { protectedstring_firstname;//里面包含三个属性,分别为_firstname、_lastname、phonenumber protectedstring_lastname; protectedstring_phonenumber;
publicProfile()//为属性定义其中的初始值 { _firstname="Saidy"; _lastname="Chen"; _phonenumber="(010)88716990"; }
publicstringgetPhonenumber()//Profile类中的方法getPhonenumber() { return_phonenumber; } publicvirtualvoidsetPhonenumber(stringphonenumber) { _phonenumber=phonenumber; }
publicstringgetFirstname()//Profile类中的方法getFirstname() { return_firstname; } publicvoidsetFirstname(stringfirstname) { _firstname=firstname; }
publicstringgetLastname()//Profile类中的方法getLastname() { return_lastname; } publicvoidsetLastname(stringlastname) { _lastname=lastname; }
publicvirtualvoidsave()//调用ISaveData接口的save()方法 { //保存数据为Text格式 FileStreamfs=newFileStream("D:\\myweb2\\profile2.txt",FileMode.Create,FileAccess.Write); StreamWritersw=newStreamWriter(fs); sw.WriteLine("Firstname:"+_firstname.ToString()); sw.WriteLine("Lastname:"+_lastname.ToString()); sw.WriteLine("Phone:"+_phonenumber.ToString()); sw.Flush(); sw.Close(); fs.Close(); } }
publicclassExtendedProfile:Profile建立Profile子类ExtendedProfile,他可以继承Profile中的方法 { protectedstring_address1;//子类ExtendedProfile的属性 protectedstring_address2; protectedstring_city; protectedstring_state; protectedstring_postal; protectedstring_description;
publicExtendedProfile()//子类ExtendedProfile中属性的初始值 { _address1="清华大学"; _address2="清华大学物理实验室"; _city="北京"; _state="北京"; _postal="100024"; _description="教授"; }
publicoverridevoidsetPhonenumber(stringphonenumber)//继承类Profile中的setPhonenumber()方法 {//setPhonenumber()方法的重载 _phonenumber=phonenumber; }
publicstringgetAddress1()//子类ExtendedProfile中的方法getAddress1(),以下类推 { return_address1; } publicstringgetAddress2() { return_address2; } publicvoidsetAddress(stringaddress1,stringaddress2) { _address1=address1; _address2=address2; }
publicstringgetCity() { return_city; } publicvoidsetCity(stringcity) { _city=city; }
publicstringgetState() { return_state; } publicvoidsetState(stringstate) { _state=state; }
publicstringgetPostal() { return_postal; } publicvoidsetPostal(stringpostal) { _postal=postal; }
publicstringgetDescription() { return_description; } publicvoidsetDescription(stringdescription) { _description=description; }
publicoverridevoidsave()//调用接口ISaveData()中的方法save(),save()方法的重载,由于多态性 {//子类ExtendedProfile可以自定义并修改save()方法 string_document="d:\\myweb2\\saidy.xml"; XmlTextWriterwriter=null;//保存为一个Xml文件 try { writer=newXmlTextWriter(_document,null); writer.Formatting=Formatting.Indented; writer.WriteStartDocument(false); writer.WriteDocType("Profile",null,null,null);//表示<!DOCTYPEProfile> writer.WriteStartElement("Profile");//生成根元素 writer.WriteElementString("firstname",_firstname);//生成子元素<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(Exceptionee) { Console.WriteLine("Exception:{0}",ee.ToString()); } } } }
|