Anonymous web server - Chapter 8 Object-Based Programming 315 52 // static
Chapter 8 Object-Based Programming 315 52 // static Count property 53 public static int Count 54 { 55 get 56 { 57 return count; 58 } 59 } 60 61 } // end class Employee Fig. 8.13 Fig. 8.13Fig. 8.FiFi13g. 8.13g. 8.13staticmembers are accessible to all objects of a class. (Part 2 of 2.) When objects of class Employeeexist, staticmember countcan be used in any method of an Employee object in this example, the constructor (lines 14 23) increments count, and the destructor (lines 26 32) decrements count. If no objects of class Employeeexist, the value of member countcan be obtained through staticproperty Count(lines 53 59); this also works when there are Employeeobjects in memory. Class StaticTest (Fig. 8.14) runs the application that demonstrates the static members of class Employee (Fig. 8.13). Lines 12 13 use the static property Count of class Employee to obtain the current count value before the program creates Employeeobjects. Notice that the syntax used to access a staticmember is: ClassName.StaticMember On line 13, ClassName is Employee and StaticMember is Count. Recall that we used this syntax in prior examples to call the staticmethods of class Math(e.g., Math.Pow, Math.Abs, etc.) and other methods, such as Int32.Parseand MessageBox.Show. Next, lines 16 17 instantiate two Employee objects and assign them to references employee1 and employee2. Each call to the Employee constructor increments the countvalue by one. Lines 19 26 display the value of Countas well as the names of the two employees. Lines 30 31 set references employee1 and employee2 to null, so they no longer refer to the Employee objects. Because these were the only references in the program to the Employeeobjects, those objects can now be garbage collected. 1 // Fig. 8.14: StaticTest.cs 2 // Demonstrating static class members. 3 4 using System; 5 6 // StaticTest class definition 7 class StaticTest 8 { 9 // main entry point for application 10 static void Main( string[] args ) 11 { 12 Console.WriteLine( “Employees before instantiation: ” + 13 Employee.Count + “n” ); 14 Fig. 8.14 Fig. 8.14Fig. 8.FiFi14g. 8.14g. 8.14staticmember demonstration. (Part 1 of 2.)
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.