给Asp.Net初学者的关于继承和多态性的例子
2024-07-10 12:56:22
供稿:网友
本人学asp.net不久,用的是c#写代码,在学习c#中发现很多初学者对继承和多态性不是特别的了解,因此作了这个例子,希望对您有所帮助,例子很简单
页面文件testprofileclass2.aspx
<%@ page language="c#" contenttype="text/html" responseencoding="gb2312" src="profile2.cs" %>
<%@ import namespace="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>
<script language="c#" runat="server">
public void page_load(object sender,eventargs e)
{
profile profile = new profile();
message.innerhtml += "<u>profile class</u><br>";
message.innerhtml += "firstname:"+profile.getfirstname()+"<br>";
message.innerhtml += "lastname:"+profile.getlastname()+"<br>";
message.innerhtml += "phone:"+profile.getphonenumber()+"<br><br>";
profile.save();
extendedprofile extendedprofile = new extendedprofile();
message.innerhtml += "<u>extendedprofile class</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>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title></title>
</head>
<body style="font:arial, helvetica, sans-serif ">
<b><h3>简单的继承/多态例子</h3></b><br><br>
object output:<br>
<br>
<div id="message" runat="server" />
</body>
</html>
类定义文件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); //表示<!doctype profile>
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(exception ee)
{
console.writeline("exception:{0}",ee.tostring());
}
}
}
}