Web hosting top - 284 Object-Based Programming Chapter 8 29 second =
284 Object-Based Programming Chapter 8 29 second = ( secondValue >= 0 && secondValue < 60 ) ? 30 secondValue : 0; 31 32 } // end method SetTime 33 34 // convert time to universal-time (24 hour) format string 35 public string ToUniversalString() 36 { 37 return String.Format( 38 "{0:D2}:{1:D2}:{2:D2}", hour, minute, second ); 39 } 40 41 // convert time to standard-time (12 hour) format string 42 public string ToStandardString() 43 { 44 return String.Format( "{0}:{1:D2}:{2:D2} {3}", 45 ( ( hour == 12 || hour == 0 ) ? 12 : hour % 12 ), 46 minute, second, ( hour < 12 ? "AM" : "PM" ) ); 47 } 48 49 } // end class Time1 Fig. 8.1 Fig. 8.1Fig. 8.FiFi1g. 8.1g. 8.1Time1abstract data type represents the time in 24-hour format. (Part 2 of 2.) In Fig. 8.1, line 7 begins the Time1 class definition, indicating that class Time1 inherits from class Object (namespace System). C# programmers use inheritance to create classes from existing classes. In fact, every class in C# (except Object) inherits from an existing class definition. On line 7, the : followed by class name Object indicates that class Time1 inherits existing pieces of class Object. If a new class definition does not specify a :and class name to the right of the new class name, the new class implicitly inherits from class Object. It is not necessary to understand inheritance to learn the concepts and programs in this chapter. We explore inheritance and class Objectin detail in Chapter 9. The opening left brace ({) at line 8 and closing right brace (}) at line 49 delineate the body of class Time1. Any information that we place in this body is said to be encapsulated (i.e., wrapped) in the class. For example, lines 9 11 of class Time1declare three intvariables hour, minute and second that represent the time of day in universal-time format (24-hour clock format). Variables declared in a class definition, but not inside a method definition, are called instance variables each instance (object) of the class contains its own separate copy of the class s instance variables. Keywords public and private are member access modifiers. Instance variables or methods with member access modifier public are accessible wherever the program has a reference to a Time1 object. However, instance variables or methods declared with member access modifier private are accessible only in that class definition. A class s publicmembers and privatemembers can be intermixed. Good Programming Practice 8.1 Every instance variable or method definition should be preceded by a member access modifier. The default access modifier for class members is private.
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.