Class Time with default constructor. : Constructor « Class « Python Tutorial






class Time:
   def __init__( self, hour = 0, minute = 0, second = 0 ):
      self.setTime( hour, minute, second )
      
   def setTime( self, hour, minute, second ):
      self.setHour( hour )
      self.setMinute( minute )
      self.setSecond( second )

   def setHour( self, hour ):
      if 0 <= hour < 24:
         self.__hour = hour
      else:
         raise ValueError, "Invalid hour value: %d" % hour

   def setMinute( self, minute ):
      if 0 <= minute < 60:
         self.__minute = minute
      else:
         raise ValueError, "Invalid minute value: %d" % minute
   
   def setSecond( self, second ):
      if 0 <= second < 60:
         self.__second = second
      else:
         raise ValueError, "Invalid second value: %d" % second

   def getHour( self ):
      return self.__hour

   def getMinute( self ):
      return self.__minute

   def getSecond( self ):
      return self.__second

   def printMilitary( self ):
      print "%.2d:%.2d:%.2d" % ( self.__hour, self.__minute, self.__second ),
 
   def printStandard( self ):
      standardTime = ""

      if self.__hour == 0 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.8.Constructor
11.8.1.specify any necessary parameters to the __init__() function
11.8.2.Constructors: __init__
11.8.3.give the constructor some parameters to work with
11.8.4.Set properties when constructed
11.8.5.Overriding the Constructor
11.8.6.If you want the base class __init__() invoked
11.8.7.Class Time with default constructor.
11.8.8.Constructor with default parameter
11.8.9.Calling Superclass Constructors