Class Time with accessor methods. : method « Class « Python Tutorial

Home
Python Tutorial
1.Introduction
2.Data Type
3.Statement
4.Operator
5.String
6.Tuple
7.List
8.Dictionary
9.Collections
10.Function
11.Class
12.File
13.Buildin Function
14.Buildin Module
15.Database
16.Regular Expressions
17.Thread
18.Tkinker
19.wxPython
20.XML
21.Network
22.CGI Web
23.Windows
Python Tutorial » Class » method 
11.4.10.Class Time with accessor methods.
class Time:
   def __init__self ):
      self._hour = 0     0-23
      self._minute = 0   0-59
      self._second = 0   0-59
      
   def setTimeself, hour, minute, second ):
      self.setHourhour )
      self.setMinuteminute )
      self.setSecondsecond )

   def setHourself, hour ):
      if <= hour < 24:
         self._hour = hour
      else:
         raise ValueError, "Invalid hour value: %d" % hour

   def setMinuteself, minute ):
      if <= minute < 60:
         self._minute = minute
      else:
         raise ValueError, "Invalid minute value: %d" % minute
   
   def setSecondself, second ):
      if <= second < 60:
         self._second = second
      else:
         raise ValueError, "Invalid second value: %d" % second

   def getHourself ):
      return self._hour

   def getMinuteself ):
      return self._minute

   def getSecondself ):
      return self._second

   def printMilitaryself ):
      print "%.2d:%.2d:%.2d" self._hour, self._minute, self._second ),
 
   def printStandardself ):
      standardTime = ""

      if self._hour == or self._hour == 12:
         standardTime += "12:"
      else:
         standardTime += "%d:" self._hour % 12 )

      standardTime += "%.2d:%.2d" self._minute, self._second )

      if self._hour < 12:
         standardTime += " AM"
      else:
         standardTime += " PM"
      
      print standardTime,
11.4.method
11.4.1.Methods in class
11.4.2.Intermediate Customization
11.4.3.Any Number of Items Iterator
11.4.4.__str__, __add__, __mul__,__nonzero__,__norm_cval,__cmp__
11.4.5.Wrapping Standard Types
11.4.6.The property() built-in function can take up to four arguments.
11.4.7.A setter
11.4.8.stick in a documentation string for your attribute
11.4.9.Here is our modified class inspired by the recipe:
11.4.10.Class Time with accessor methods.
11.4.11.Methods calling
11.4.12.Add method to a class from outside
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.