程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 給Asp.Net初學者的關於繼承和多態性的例子

給Asp.Net初學者的關於繼承和多態性的例子

編輯:.NET實例教程


本人學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
  {
    pro
tected 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中屬性的初始值
&n
bsp;{
   _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; &n
bsp;         //保存為一個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());
   }
 }
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved