给Asp.Net初学者的关于继承和多态性的例子(1)
2024-07-10 13:11:11
供稿:网友
本人学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 >
<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();
}
}