Object-Based Programming--python0.0.0.1

<<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  

 

 

 

 

 

转载于:https://www.cnblogs.com/caidaodaxia/archive/2010/08/24/1806908.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值