<<DEITEL - Python How to Program 2002>>

7.2 Implementing a Time Abstract Data Type with a Class
Classes enable programmers to model objects that have data (represented as attributes) and behaviors—or operations—(represented as methods). Methods are invoked in response to messages sent to objects. A message corresponds to a method call sent from one object to
another.
Classes simplify programming because the clients (or users of the class) need to be concerned only with the operations encapsulated or embedded in the object—the object interface. Such operations usually are designed to be client-oriented rather than implementation-
oriented. Clients do not need to be concerned with a class’s implementation (although clients, of course, want correct and efficient implementations). When an implementation changes, implementation-dependent code must change accordingly. Hiding the implementation eliminates the possibility of other program parts becoming dependent on the details of the class implementation.

Often, classes do not have to be created “from scratch.” Rather, they may be derived from other classes that provide attributes and behaviors the new classes can use—or classes can include objects of other classes as members. Such software reuse can greatly enhance programmer productivity. Deriving new classes from existing classes is called inheritance and is discussed in detail in Chapter 9, Object-Oriented Programming: Inheritance.


Figure 7.1 contains a simple definition for class Time. The class contains information that describes the time of day and contains methods for printing the time in two formats.
The class maintains the time internally in a 24-hour format (i.e., military time), but allows the client to display the time in either 24-hour format or in “standard” (AM, PM) format.
Later in this section, we present a program (Fig. 7.2) that demonstrates how to create an object of class Time.
Keyword class (line 4) begins a class definition. The keyword is followed by the name of the class (Time), which is followed by a colon (:). The line that contains keyword class and the class name is called the class’s header.

The body of the class is an indented code block (lines 5–37) that contains methods and attributes that belong to the class. Class names usually follow the same naming conventions as variable names, except that the first word of the class name is capitalized.

 

 

 

1 # Fig. 7.1: Time1.py
2 # Simple definition of class Time.
3
4  class Time:
5 """Time abstract data type (ADT) definition"""
6
7 def __init__( self ):
8 """Initializes hour, minute and second to zero"""
9
10 self.hour = 0 # 0-23
11 self.minute = 0 # 0-59
12 self.second = 0 # 0-59
13
14 def printMilitary( self ):
15 """Prints object of class Time in military format"""
16
17 print "%.2d:%.2d:%.2d" % \
18 ( self.hour, self.minute, self.second ),
19
20 def printStandard( self ):
21 """Prints object of class Time in standard format"""
22
23 standardTime = ""
24
25 if self.hour == 0 or self.hour == 12:
26 standardTime += "12:"
27 else:
28 standardTime += "%d:" % ( self.hour % 12 )
29
30 standardTime += "%.2d:%.2d" % ( self.minute, self.second )
31
32 if self.hour < 12:
33 standardTime += " AM"
34 else:
35 standardTime += " PM"
36
37 print standardTime,
38
39 ##########################################################################
40 # (C) Copyright 2002 by Deitel & Associates, Inc. and Prentice Hall. #
41 # All Rights Reserved. #
42 # #
43 # DISCLAIMER: The authors and publisher of this book have used their #
44 # best efforts in preparing the book. These efforts include the #
45 # development, research, and testing of the theories and programs #
46 # to determine their effectiveness. The authors and publisher make #
47 # no warranty of any kind, expressed or implied, with regard to these #
48 # programs or to the documentation contained in these books. The authors #
49 # and publisher shall not be liable in any event for incidental or #
50 # consequential damages in connection with, or arising out of, the #
51 # furnishing, performance, or use of these programs. #
52 ##########################################################################
53  

 

 

 

 

 

posted on 2010-08-24 02:02  菜刀大侠  阅读(278)  评论(0编辑  收藏  举报