Examples of inheritance and polymorphism for asp.net beginners (2)
Last Update:2017-02-28
Source: Internet
Author: User
Asp.net| Beginners | Inherit public class Extendedprofile:profile build 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";
}
public override void Setphonenumber (string phonenumber)//Inheritance Class Profile Setphonenumber () method
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;
}
public override void Save ()//Invoke the Method Save () in the 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"); Generate root element
writer. WriteElementString ("FirstName", _FirstName); Generating 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 ());
}
}
}
}