I learned Asp. net soon, I used C # To write code. I learned C # and found that many beginners do not have a special understanding of inheritance and polymorphism. So I gave this example and hope to help you, the example is simple.
Page file 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;
}
</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> ";
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> Object output: <br>
<Br>
<Div id = "message" runat = "server"/>
</Body>
</Html>
Class definition file profile2.cs
Using System;
Using System. IO;
Using System. Xml;
Namespace shai // namespace shai
{
Interface ISaveData // defines an interface ISaveData, which is inherited as the base class of the Profile and can be
{// Call the methods in different classes and modify
Void save ();
}
Public class Profile: ISaveData // defines the class Profile
{
Protected string _ firstname; // contains three attributes: _ firstname, _ lastname, and phonenumber.
Protected string _ lastname;
Protected string _ phonenumber;
Public Profile () // defines the initial value for the attribute
{
_ Firstname = "Saidy ";
_ Lastname = "Chen ";
_ Phonenumber = "(010) 88716990 ";
}
Public string getPhonenumber () // method in the Profile class getPhonenumber ()
{
Return _ phonenumber;
}
Public virtual void setPhonenumber (string phonenumber)
{
_ Phonenumber = phonenumber;
}
Public string getFirstname () // method in the Profile class getFirstname ()
{
Return _ firstname;
}
Public void setFirstname (string firstname)
{
_ Firstname = firstname;
}
Public string getLastname () // method in the Profile class getLastname ()
{
Return _ lastname;
}
Public void setLastname (string lastname)
{
_ Lastname = lastname;
}
Public virtual void save () // call the save () method of the ISaveData Interface
{
// Save the data in Text format
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: Creates the Profile subclass ExtendedProfile, which can inherit the methods in Profile
{
Protected string _ address1; // attributes of the subclass ExtendedProfile
Protected string _ address2;
Protected string _ city;
Protected string _ state;
Protected string _ postal;
Protected string _ description;
Public ExtendedProfile () // the initial value of the attribute in the subclass ExtendedProfile
{
_ Address1 = "Tsinghua University ";
_ Address2 = "Physical Laboratory of Tsinghua University ";
_ City = "Beijing ";
_ State = "Beijing ";
_ Postal = "100024 ";
_ Description = "professor ";
}
Public override void setPhonenumber (string phonenumber) // inherit the setPhonenumber () method in the Profile class
{// SetPhonenumber () method overload
_ Phonenumber = phonenumber;
}
Public string getAddress1 () // method getAddress1 () in the subclass ExtendedProfile, and so on
{
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 () // call the methods save () and save () in the ISaveData () interface because of Polymorphism
{// Subclass ExtendedProfile can customize and modify the save () method
String _ document = "d :\\ myweb2 \ saidy. xml ";
XmlTextWriter writer = null; // save as an Xml file
Try
{
Writer = new XmlTextWriter (_ document, null );
Writer. Formatting = Formatting. Indented;
Writer. WriteStartDocument (false );
Writer. WriteDocType ("Profile", null); // indicates <! DOCTYPE Profile>
Writer. WriteStartElement ("Profile"); // generate the root element
Writer. WriteElementString ("firstname", _ firstname); // generates a child element <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 ());
}
}
}
}