Examples of inheritance and polymorphism for asp.net beginners (2)

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 ());


   }


   }


  }


  }





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.