1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| class MyTime(object) : 'MyTime - operate hours, minutes and seconds' def __init__(self, h, m, s) : 'MyTime - Constructor' self.hour = h; self.min = m; self.sec = s;
def __str__(self) : 'MyTime - string representation' return '%d : %d : %d' % (self.hour, self.min, self.sec) __repr__ = __str__ def __add__(self, time) : 'MyTime - overloading the addition operator' h = m = s = 0 t = self.sec + time.sec s = t % 60 m += t /60 t = self.min + time.min + m m = t % 60 h = t / 60 t = self.hour + time.hour + h h = t % 24 return self.__class__(h, m, s)
def __iadd__(self, time) : 'MyTime - overloading the in-place addition operator' t = self.sec + time.sec self.sec = t % 60 self.min += t / 60 t = self.min + time.min self.min = t % 60 self.hour += t /60 t = self.hour + time.hour self.hour = t % 24 return self
t1 = MyTime(12,34,57) t2 = MyTime(8,6,56) print 't1 >> ', t1 print 't2 >> ', t2 print 't1 + t2 >> ', t1 + t2 t1 += t2 print 't1 += t2 >> ', t1
|