Examples of inheritance and polymorphism for asp.net beginners-practical tips

Source: Internet
Author: User

I learned asp.net soon, using C # to write code, in the study of C # found many beginners to inheritance and polymorphism is not a special understanding, so this example, I hope to help you, the example is very simple

Paging File testprofileclass2.aspx
<%@ Page language= "C #" contenttype= "text/html" responseencoding= "gb2312" src= "Profile2.cs"%>
<%@ Import namespace= "Shai"%>


<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 profiles = 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>

<body style= "font:arial, Helvetica, Sans-serif" >
<b>Object output:<br>
<br>
<div id= "message" runat= "Server"/>
</body>


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 profile, because of its polymorphic nature, can be
{//Different classes call the methods in them, and modify the
void Save ();
}

public class Profile:isavedata//Definition class profile
{
protected string _FirstName; It contains three properties, namely _FirstName, _lastname, PhoneNumber
protected string _lastname;
protected string _PhoneNumber;

Public profile ()//defines the initial value for the property
{
_FirstName = "Saidy";
_lastname = "Chen";
_PhoneNumber = "(010) 88716990";
}

The method Getphonenumber () in the public string Getphonenumber ()//profile class
{
return _phonenumber;
}
public virtual void Setphonenumber (string phonenumber)
{
_PhoneNumber = PhoneNumber;
}

The method Getfirstname () in the public string Getfirstname ()//profile class
{
return _firstname;
}
public void Setfirstname (string firstname)
{
_FirstName = FirstName;
}

The method Getlastname () in the public string Getlastname ()//profile class
{
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 establishes the profile subclass Extendedprofile, he can inherit the method in profile
{
protected string _address1; Properties of Subclass Extendedprofile
protected string _address2;
protected string _city;
protected string _state;
protected string _postal;
protected string _description;

The initial value of the property in the public extendedprofile ()//Subclass Extendedprofile
{
_address1 = "Tsinghua University";
_ADDRESS2 = "Physics Laboratory of Tsinghua University";
_city = "Beijing";
_state = "Beijing";
_postal = "100024";
_description = "Professor";
}

The Setphonenumber () method in the public override void Setphonenumber (string phonenumber)//Inheritance class profile
The overload of the {//setphonenumber () method
_PhoneNumber = PhoneNumber;
}

The method GetAddress1 () in the public string GetAddress1 ()//subclass Extendedprofile, and the following analogy
{
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;
}

The method Save () in the public override void Save ()//Calling Interface Isavedata (), the overload of the Save () method, due to 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,null,null); Indicates <! DOCTYPE profile>
Writer.                         WriteStartElement ("Profile"); Generating the root element
Writer.   WriteElementString ("FirstName", _FirstName); Building child elements <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 ());
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.