Time Class Case Study

Một phần của tài liệu Visual C 2012 How to Program _ www.bit.ly/taiho123 (Trang 413 - 417)

2. When the app executes, another compiler (known as the just-in-time compiler

10.2 Time Class Case Study

Time1Class Declaration

Our first example consists of classesTime1(Fig. 10.1) andTime1Test(Fig. 10.2). Class

Time1represents the time of day. ClassTime1Test’sMainmethod creates an object of class

Time1and invokes its methods. The output of this app appears in Fig. 10.2. [Note: C# has the typeDateTimefor date and time manipulations. Our time examples are fordemonstra- tionpurposes—you do not need to create your own types for dates and times.]

ClassTime1contains threeprivateinstance variables of typeint(Fig. 10.1, lines 7–

9)—hour,minuteandsecond—that represent the time in universal-time format (24-hour clock format, in which hours are in the range 0–23). ClassTime1containspublicmethods

SetTime(lines 13–25), ToUniversalString(lines 28–32) andToString(lines 35–40).

These are thepublicservicesor thepublicinterfacethat this class provides to its clients.

10.1 Introduction

10.2 TimeClass Case Study 10.3 Controlling Access to Members 10.4 Referring to the Current Object’s

Members with thethisReference 10.5 TimeClass Case Study: Overloaded

Constructors

10.6 Default and Parameterless Constructors 10.7 Composition

10.8 Garbage Collection and Destructors 10.9 staticClass Members

10.10 readonlyInstance Variables 10.11 Data Abstraction and

Encapsulation

10.12 Class ViewandObject Browser

10.13 Object Initializers 10.14 Wrap-Up

Summary | Terminology | Self-Review Exercises | Answers to Self-Review Exercises | Exercises

1 // Fig. 10.1: Time1.cs

2 // Time1 class declaration maintains the time in 24-hour format.

3 using System; // namespace containing ArgumentOutOfRangeException 4

5 public class Time1 6 {

7 8 9

Fig. 10.1 | Time1class declaration maintains the time in 24-hour format. (Part 1 of 2.)

private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59

10.2 TimeClass Case Study 373

In this example, classTime1does not declare a constructor, so the class has a default constructor that’s supplied by the compiler. Each instance variable implicitly receives the default value0for anint. When instance variables are declared in the class body, they can be initialized using the same initialization syntax as a local variable.

MethodSetTimeand Throwing Exceptions

MethodSetTime(lines 13–25) is apublicmethod that declares threeintparameters and uses them to set the time. Lines 16–17 test each argument to determine whether the value is in the proper range, and, if so, lines 19–21 assign the values to thehour,minuteand

secondinstance variables. Thehourvalue (line 13) must be greater than or equal to0and less than24, because universal-time format represents hours as integers from 0 to 23 (e.g., 1 PM is hour 13 and 11 PM is hour 23; midnight is hour 0 and noon is hour 12). Similarly, bothminuteandsecondvalues must be greater than or equal to0and less than60. For val- ues outside these ranges,SetTimethrows an exceptionof typeArgumentOutOfRangeEx- ception(lines 23–24), which notifies the client code that an invalid argument was passed to the method. As you learned in Chapter 8, you can usetry...catchto catch exceptions and attempt to recover from them, which we’ll do in Fig. 10.2. Thethrowstatement(line 10

11 // set a new time value using universal time; throw an 12 // exception if the hour, minute or second is invalid 13 public void SetTime( int h, int m, int s )

14 {

15 // validate hour, minute and second

16 if ( ( h >= 0 && h < 24 ) && ( m >= 0 && m < 60 ) &&

17 ( s >= 0 && s < 60 ) )

18 {

19 = h;

20 = m;

21 = s;

22 } // end if

23 else

24

25 } // end method SetTime 26

27 // convert to string in universal-time format (HH:MM:SS) 28 public string ToUniversalString()

29 {

30 31

32 } // end method ToUniversalString 33

34 // convert to string in standard-time format (H:MM:SS AM or PM) 35 public override string ToString()

36 {

37 38 39

40 } // end method ToString 41 } // end class Time1

Fig. 10.1 | Time1class declaration maintains the time in 24-hour format. (Part 2 of 2.)

hour minute second

throw new ArgumentOutOfRangeException();

return string.Format( "{0:D2}:{1:D2}:{2:D2}", hour, minute, second );

return string.Format( "{0}:{1:D2}:{2:D2} {3}", ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ), minute, second, ( hour < 12 ? "AM" : "PM" ) );

24) creates a new object of typeArgumentOutOfRangeException. The parentheses follow- ing the class name indicate a call to theArgumentOutOfRangeExceptionconstructor. After the exception object is created, thethrowstatement immediately terminates methodSet-

Timeand the exception is returned to the code that attempted to set the time.

MethodToUniversalString

MethodToUniversalString(lines 28–32) takes no arguments and returns astringin universal-time format, consisting of six digits—two for the hour, two for the minute and two for the second. For example, if the time were 1:30:07 PM, methodToUniversal- Stringwould return13:30:07. Thereturnstatement (lines 30–31) usesstaticmethod

Formatof classstringto return astringcontaining the formattedhour,minuteandsec-

ondvalues, each with two digits and, where needed, a leading0(specified with theD2for- mat specifier—which pads the integer withleading0sif it has less than two digits). Method

Formatis similar to thestringformatting in methodConsole.Write, except thatFormat returns a formattedstringrather than displaying it in a console window. The formatted

stringis returned by methodToUniversalString. MethodToString

MethodToString(lines 35–40) takes no arguments and returns astring in standard- time format, consisting of thehour,minuteandsecondvalues separated by colons and fol- lowed by an AM or PM indicator (e.g.,1:27:06 PM). Like methodToUniversalString, methodToStringusesstatic stringmethodFormatto format theminuteandsecond as two-digit values with leading0s, if necessary. Line 38 uses a conditional operator (?:) to determine the value forhourin the string—if thehouris0or12(AM or PM), it appears as 12—otherwise, it appears as a value from 1 to 11. The conditional operator in line 39 determines whether AM or PM will be returned as part of thestring.

Recall from Section 7.4 that all objects in C# have aToStringmethod that returns a

stringrepresentation of the object. We chose to return astringcontaining the time in standard-time format. Method ToString is called implicitly when an object’s value is output with a format item in a call toConsole.Write. Remember that to enable objects to be converted to theirstringrepresentations, we need to declare methodToStringwith keywordoverride—the reason for this will become clear when we discussinheritancein Chapter 11.

Using ClassTime1

As you learned in Chapter 4, each class you declare represents a newtypein C#. Therefore, after declaring classTime1, we can use it as a type in declarations such as

TheTime1Testapp class (Fig. 10.2) uses classTime1. Line 10 creates aTime1object and assigns it to local variabletime. Operatornewinvokes classTime1’sdefault constructor, sinceTime1does not declare any constructors. Lines 13–17 output the time, first in uni- versal-time format (by invokingtime’sToUniversalStringmethod in line 14), then in standard-time format (by explicitly invokingtime’sToStringmethod in line 16) to con- firm that theTime1object was initialized properly. Line 20 invokes methodSetTimeof thetimeobject to change the time. Then lines 21–25 output the time again in both for- mats to confirm that the time was set correctly.

Time1 sunset; // sunset can hold a reference to a Time1 object

10.2 TimeClass Case Study 375

1 // Fig. 10.2: Time1Test.cs 2 // Time1 object used in an app.

3 using System;

4

5 public class Time1Test 6 {

7 public static void Main( string[] args )

8 {

9 // create and initialize a Time1 object 10

11

12 // output string representations of the time 13 Console.Write( "The initial universal time is: " );

14 Console.WriteLine( );

15 Console.Write( "The initial standard time is: " );

16 Console.WriteLine( );

17 Console.WriteLine(); // output a blank line 18

19 // change time and output updated time 20

21 Console.Write( "Universal time after SetTime is: " );

22 Console.WriteLine( );

23 Console.Write( "Standard time after SetTime is: " );

24 Console.WriteLine( );

25 Console.WriteLine(); // output a blank line 26

27 // attempt to set time with invalid values

28 try

29 {

30

31 } // end try

32 catch ( ArgumentOutOfRangeException ex )

33 {

34 Console.WriteLine( ex.Message + "\n" );

35 } // end catch 36

37 // display time after attempt to set invalid values 38 Console.WriteLine( "After attempting invalid settings:" );

39 Console.Write( "Universal time: " );

40 Console.WriteLine( );

41 Console.Write( "Standard time: " );

42 Console.WriteLine( );

43 } // end Main

44 } // end class Time1Test

The initial universal time is: 00:00:00 The initial standard time is: 12:00:00 AM Universal time after SetTime is: 13:27:06 Standard time after SetTime is: 1:27:06 PM

Specified argument was out of the range of valid values.

Fig. 10.2 | Time1object used in an app. (Part 1 of 2.)

Time1 time = new Time1(); // invokes Time1 constructor

time.ToUniversalString() time.ToString()

time.SetTime( 13, 27, 6 );

time.ToUniversalString() time.ToString()

time.SetTime( 99, 99, 99 );

time.ToUniversalString() time.ToString()

CallingTimeMethodSetTimewith Invalid Values

To illustrate that methodSetTimevalidates its arguments, line 30 calls methodSetTime

with invalid arguments of99for thehour,minuteandsecond. This statement is placed in a try block (lines 28–31) in case SetTime throws an ArgumentOutOfRangeException, which it will do since the arguments are all invalid. When this occurs, the exception is caught at lines 32–35 and the exception’sMessageproperty is displayed. Lines 38–42 out- put the time again in both formats to confirm thatSetTimedid not change the time when invalid arguments were supplied.

Notes on theTime1Class Declaration

Consider several issues of class design with respect to classTime1. The instance variables

hour,minuteandsecondare each declaredprivate. The actual data representation used within the class is of no concern to the class’s clients. For example, it would be perfectly reasonable forTime1to represent the time internally as the number of seconds since mid- night or the number of minutes and seconds since midnight. Clients could use the same

public methods and properties to get the same results without being aware of this.

(Exercise 10.4 asks you to represent the time as the number of seconds since midnight and show that indeed no change is visible to the clients of the class.)

Một phần của tài liệu Visual C 2012 How to Program _ www.bit.ly/taiho123 (Trang 413 - 417)

Tải bản đầy đủ (PDF)

(1.020 trang)