Example Analysis of inheritance and Polymorphism in Asp.Net
This article mainly shows you the "sample Analysis of inheritance and Polymorphism in Asp.Net", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "sample Analysis of inheritance and Polymorphism in Asp.Net".
Page file TestProfileClass2.aspx
P {font:Arial, Helvetica, sans-serif;background-color:#cccccc;border-color:black;border-width:1;border-style:solid;padding:10,10,10,10;} public void Page_Load (Object sender,EventArgs e) {Profile profile = new Profile (); message.InnerHtml + = "Profile Class
; message.InnerHtml + = "Firstname:" + profile.getFirstname () + "
; message.InnerHtml + = "Lastname:" + profile.getLastname () + "
; message.InnerHtml + = "Phone:" + profile.getPhonenumber () + "
"; profile.save (); ExtendedProfile 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 ();} simple inheritance / polymorphic examples
Object output:
The class definition file profile2.csusing 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 polymorphism, it can be called by {/ / different classes and modify void save () } public class Profile:ISaveData / / definition class Profile {protected string _ firstname; / / contains three attributes, namely _ firstname, _ lastname, phonenumber protected string _ lastname; protected string _ phonenumber; public Profile () / / define the initial value {_ firstname = "Saidy"; _ lastname = "Chen"; _ phonenumber = "(010) 88716990" for the attribute Method getPhonenumber () {return _ phonenumber;} public virtual void setPhonenumber (string phonenumber) {_ phonenumber = phonenumber;} public string getFirstname () / / method getFirstname () {return _ firstname;} public void setFirstname (string firstname) {_ firstname = firstname in the} public string getPhonenumber () / / Profile class } method 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 establishes the Profile subclass ExtendedProfile, which can inherit the method {protected string _ address1 in Profile / / attributes of subclass ExtendedProfile protected string _ address2; protected string _ city; protected string _ state; protected string _ postal; protected string _ description; public ExtendedProfile () / initial values of attributes in 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) / / overloaded _ phonenumber = phonenumber of the setPhonenumber () method {/ / setPhonenumber () method in the inheritance class Profile The method getAddress1 () in the subclass ExtendedProfile {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 method save () in interface ISaveData (), overload of save () method, due to polymorphism {/ / subclass ExtendedProfile can customize and modify save () method string _ document = "d:\\ myweb2\\ saidy.xml"; XmlTextWriter writer = null / Save as a Xml file try {writer = new XmlTextWriter (_ document,null); writer.Formatting = Formatting.Indented; writer.WriteStartDocument (false); writer.WriteDocType ("Profile", null,null,null); / / means writer.WriteStartElement ("Profile"); / / generate the root element writer.WriteElementString ("firstname", _ firstname) / generate child elements _ 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 ());} these are all the contents of the article "sample Analysis of inheritance and Polymorphism in Asp.Net". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!