Examples of inheritance and polymorphism for asp.net beginners (1)
Last Update:2017-02-28
Source: Internet
Author: User
Asp.net| Beginners | Inherit I learn asp.net soon, with the C # write code, in learning C # found many beginners to inheritance and polymorphism is not a special understanding, so this example, I hope to help you, examples are very simple
Paging 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,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>
</head>
<body style= "font:arial, Helvetica, Sans-serif" >
<b><h3> Simple inheritance/polymorphism example </h3></b><br><br>
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 profile, and can be
because of its polymorphic nature
{//Different classes call the methods, 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";
}
public String Getphonenumber ()//profile method in class Getphonenumber ()
{
return _phonenumber;
}
public virtual void Setphonenumber (string phonenumber)
{
_phonenumber = PhoneNumber;
}
public String Getfirstname ()//profile method in Class Getfirstname ()
{
return _firstname;
}
public void Setfirstname (string firstname)
{
_firstname = FirstName;
}
public String Getlastname ()//profile method in Class Getlastname ()
{
return _lastname;
}
public void Setlastname (string lastname)
{
_lastname = LastName;
}
public virtual void Save ()//Call the Save () method of the Isavedata interface
{
//Save data as 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 ();
}
}