许多学习Asp.Net的新人对Asp.Net继承和多态性不是特别的了解,但其在编程中有重要作用,为了帮助大家学习,我们作了这个例子,以实例来说明,代码比较简单。
以下为引用的内容:
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="清华大学物理实验室";