给Asp.Net初学者的关于继承和多态性的例子

2008-02-22 09:30:19来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

本人学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()方法的重载

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ASP.NET中用healthMonitor属性用法

下一篇:调试ASP.NET应用程序的方法和技巧