usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacedemo5{ Public Partial classGongxiang:System.Web.UI.Page { Public classUser {Private Static intCount//define static variables Count Private Static stringName//defining static variables name PublicUser ()//default constructor{Count++; } StaticUser ()//Static Constructors{Count=0; Name="Initial value"; } Public stringName//Name Attribute { Get { returnname;} Set{Name=value; } } Public Static stringOnename//Static Properties { Get { returnname;} } Public Static intCount//how many objects are defined with this class { Get { returnCount//We only know this number, so read only } } Public Static stringMn (stringnname) { return "object, the result of sharing a method is;"+Nname; } } protected voidPage_Load (Objectsender, EventArgs e) {Response.Write ("The present is:"+user.count +"object, the initial name is:"+user.onename); User U1=NewUser (); U1. Name="days of the boom wear"; Response.Write ("<br> currently this is"+user.count +User. Mn (U1. Name)); User U2=NewUser (); U2. Name="Tianhongchuan"; Response.Write ("<br> currently this is"+ User.count +User.mn (U2. Name)); User U3=NewUser (); U2. Name="Huangxiaomei"; Response.Write ("<br> currently this is"+ User.count +user.mn (U3. Name)); } }}
Results:
If there is no instantiation, call the static constructor and assign the initial value to the variable.
If instantiated, it is executed according to the General constructor. After instantiating a class, you can access static methods, although you cannot access other static variables.
Shared variable--[asp.net]