Class Time with accessor methods. : Method : Class PYTHON TUTORIALS


PYTHON TUTORIALS » Class » Method »

 

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,



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Class
» Method