Chapter 8 Object-Based Programming (Web hosting e commerce) 289 Line 23 sets
Chapter 8 Object-Based Programming 289 Line 23 sets the time for the Time1 object to which time refers by passing valid hour, minute and second arguments to Time1 method SetTime. Lines 26 29 append to outputthe new time in both universal and standard formats to confirm that the time was set correctly. To illustrate that method SetTime validates the values passed to it, line 32 passes invalid time arguments to method SetTime. Lines 34 36 append to output the new time in both formats. All three values passed to SetTime are invalid, so instance variables hour, minute and second are set to 0. Line 38 displays a MessageBox with the results of our program. Notice in the last two lines of the output window that the time was indeed set to midnight when invalid arguments were passed to SetTime. Time1 is our first example of a class that does not contain method Main. Thus, class Time1 cannot be used to begin program execution. Class TimeTest1 defines a Main method, so class TimeTest1 can be used to begin program execution. A class containing method Main also is known as the entry point into the program. Note that the program declares instance variables hour, minute and second as private. Such instance variables are not accessible outside the class in which they are defined. A class s clients should not be concerned with the data representation of that class. Clients of a class should be interested only in the services provided by that class. For example, the class could represent the time internally as the number of seconds that have elapsed since the previous midnight. Suppose the data representation changes. Clients still are able to use the same public methods and obtain the same results without being aware of the change in internal representation. In this sense, the implementation of a class is said to be hidden from its clients. Software Engineering Observation 8.7 Information hiding promotes program modifiability and simplifies the client s perception of a class. Software Engineering Observation 8.8 Clients of a class can (and should) use the class without knowing the internal details of how the class is implemented. If the class implementation changes (to improve performance, for example), but the class interface remains constant, the client s source code need not change. This makes it much easier to modify systems. In this program, the Time1 constructor initializes the instance variables to 0 (the universal time equivalent of 12 AM) to ensure that the object is created in a consistent state i.e., all instance variables have valid values. The instance variables of a Time1 object cannot store invalid values, because the constructor, which calls SetTime, is called to initialize the instance variables when the Time1 object is created. Method SetTime scrutinizes subsequent attempts by a client to modify the instance variables. Normally, the instance variables of a class are initialized in that class s constructor, but they also can be initialized when they are declared in the class body. If a programmer does not initialize instance variables explicitly, the compiler implicitly initializes them. When this occurs, the compiler sets primitive numeric variables to 0, bool values to false and references to null. Methods ToUniversalString and ToStandardString take no arguments, because, by default, these methods manipulate the instance variables of the particular Time1object on which they are invoked. This often makes method calls more concise than
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.