Chapter 8 Object-Based Programming 301 Lines 57 69, 72 84 (Web hosting script)

Chapter 8 Object-Based Programming 301 Lines 57 69, 72 84 and 87 99 define Time3properties Hour, Minuteand Second, respectively. Each property begins with a declaration line that includes the property s access modifier (public), type (int) and name (Hour, Minuteor Second). The body of each property contains get and set accessors, which are declared using the reserved words getand set. The getaccessor declarations are on lines 59 62, 74 77 and 89 92. These accessors return the hour, minuteand secondinstance variable values that objects request. The setaccessors are declared on lines 64 67, 79 82 and 94 97. The body of each setaccessor performs the same conditional statement that was previously performed by method SetTimeto set the hour, minuteor second. Method SetTime(lines 48 54) now uses properties Hour, Minuteand Secondto ensure that instance variables hour, minute and second have valid values. After we define a property, we can use it in the same way that we use a variable. We assign values to properties using the = (assignment) operator. When this assignment occurs, the code in the setaccessor for that property executes. The reserved word valuerepresents the argument to the setaccessor. Similarly, methods ToUniversalString(102 106) and ToStandardString (109 114) now use properties Hour, Minute and Second to obtain the values of instance variables hour, minuteand second. Referencing the property executes the getaccessor for that property. When we use set and get accessor methods throughout the constructors and other methods of class Time3, we minimize the changes that we must make to the class definition in the event that we alter the data representation from hour, minute and second to another representation (such as total elapsed seconds in the day). When such changes are made, we must provide only new set and get accessor bodies. Using this technique also enables programmers to change the implementation of a class without affecting the clients of that class (as long as all the publicmethods of the class still are called in the same way). Software Engineering Observation 8.15 Accessing private data through set and get accessors not only protects the instance variables from receiving invalid values, but also hides the internal representation of the instance variables from that class s clients. Thus, if representation of the data changes (typically, to reduce the amount of required storage or to improve performance), only the method implementations need to change the client implementations need not change, as long as the interface provided by the methods is preserved. Class TimeTest3 (Fig. 8.7) defines an application with a GUI for manipulating an object of class Time3. [Note: We do not show Visual Studio s Windows Form Designer generated code. Instead, line 45 provides a comment to indicate where the generated code appears in the source code file. You can view this code on the CD that accompanies this book.] 1 // Fig. 8.7: TimeTest3.cs 2 // Demonstrating Time3 properties Hour, Minute and Second. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; Fig. 8.7 Fig. 8.7Fig. 8.FiFi7g. 8.7g.8.7Properties demonstration for class Time3. (Part 1 of 5.)
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Leave a Reply