I learned ASP. NET soon and used C # To writeCodeIn learning C #, I found that many beginners do not have a special understanding of inheritance and polymorphism. Therefore, I have provided this example to help you. The example is very 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 = new extendedprofile ();
message. innerhtml + = " extendedprofile class
";
message. innerhtml + = "firstname:" + profile. getfirstname () + "
";
message. innerhtml + = "lastname:" + profile. getlastname () + "
";
message. innerhtml + = "Phone:" + extendedprofile. getphonenumber () + "
";
message. innerhtml + = "address1:" + extendedprofile. getaddress1 () + "
";
message. innerhtml + = "address2:" + extendedprofile. getaddress2 () + "
";
message. innerhtml + = "City:" + extendedprofile. getcity () + "
";
message. innerhtml + = "state:" + extendedprofile. getstate () + "
";
message. innerhtml + = "Postal:" + extendedprofile. getpostal () + "
";
message. innerhtml + = "Description:" + extendedprofile. getdescription () + "
";
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 ());
}
}
}
}