題目:
定義一個表示時間的類Time
a)Time(hours,minutes,seconds)建立一個時間對象;
b)t.hours(),t.minutes(),t.seconds()分別返回時間對象t的小時,分鐘和秒值
c)爲Time對象定義加法和減法操做(用運算符+和-)
d)定義時間對象的等於和小於關係對象(用運算符==和<)
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 """ 5 定義一個表示時間的類Time 6 a)Time(hours,minutes,seconds)建立一個時間對象; 7 b)t.hours(),t.minutes(),t.seconds()分別返回時間對象t的小時,分鐘和秒值 8 c)爲Time對象定義加法和減法操做(用運算符+和-) 9 d)定義時間對象的等於和小於關係對象(用運算符==和<) 10 11 ADT Time: #定義時間的抽象數據類型 12 Time(self, int hours, int minutes, int seconds) #構造時間對象 13 +(self, Time t2) #求出本對象加t2的結果 14 -(self, Time t2) #求出本對象減t2的結果 15 ==(self, Time t2) #本對象是否和t2相等 16 <(self, Time t2) #本對象小於t2 17 hours(self) #取出本對象的小時 18 minutes(self) #取出本對象的分鐘 19 seconds(self) #取出本對象的秒值 20 """ 21 """ 22 Author: Minion Xu 23 Time:2016-11-21 24 """ 25 26 class Time(object): 27 __slots__ = ('_hours','_minutes','_seconds') 28 29 #建立時間對象 30 def __init__(self, hours=0, minutes=0, seconds=0): 31 #類型判斷 32 if not isinstance(hours, int) or not isinstance(minutes, int) or not isinstance(seconds, int): 33 raise TypeError 34 35 if(0<=hours<=23): 36 self._hours = hours 37 if (0 <= minutes <= 59): 38 self._minutes = minutes 39 if (0 <= seconds <= 59): 40 self._seconds = seconds 41 else: 42 raise ValueError("%d is not valid seconds!" % seconds) 43 else: 44 raise ValueError("%d is not valid minutes!" % minutes) 45 46 else: 47 raise ValueError("%d is not valid hours!" % hours) 48 49 50 # + 51 def __add__(self, other): 52 seconds_add = self._seconds + other._seconds 53 seconds_carry = seconds_add // 60 #超過60秒進位 54 seconds = seconds_add % 60 #秒值 55 minutes_add = self._minutes + other._minutes + seconds_carry 56 minutes_carry = minutes_add // 60 #超過60分進位 57 minutes = minutes_add % 60 #分鐘 58 hours_add = self._hours + other._hours + minutes_carry 59 if(hours_add<24): 60 hours = hours_add 61 else: 62 hours = hours_add - 24 #小時 63 return Time(hours, minutes, seconds) 64 # - 65 def __sub__(self, other): 66 if self._seconds < other._seconds: 67 self._minutes -= 1 68 seconds = self._seconds + 60 - other._seconds 69 else: 70 seconds = self._seconds - other._seconds 71 if self._minutes < other._minutes: 72 self._hours -= 1 73 minutes = self._minutes + 60 - other._minutes 74 else: 75 minutes = self._minutes - other._minutes 76 if self._hours < other._hours: 77 hours = self._hours + 24 - other._hours 78 else: 79 hours = self._hours - other._hours 80 return Time(hours, minutes, seconds) 81 # == 82 def __eq__(self, other): 83 bool1 = False 84 bool2 = False 85 bool3 = False 86 if self._hours == other._hours: 87 bool1 = True 88 if self._minutes == other._minutes: 89 bool2 = True 90 if self._seconds == other._seconds: 91 bool3 = True 92 return bool1 and bool2 and bool3 93 94 # < 95 def __lt__(self, other): 96 if self._hours < other._hours: 97 return True 98 elif self._hours == other._hours: 99 if self._minutes < other._minutes: 100 return True 101 elif self._minutes == other._minutes: 102 if self._seconds < other._seconds: 103 return True 104 else: 105 return False 106 else: 107 return False 108 else: 109 return False 110 111 def hours(self): 112 return self._hours 113 114 def minutes(self): 115 return self._minutes 116 117 def seconds(self): 118 return self._seconds 119 120 def print(self): 121 print(self._hours,":",self._minutes,":",self._seconds) 122 123 def __str__(self): 124 return str(self._hours) + ":" + str(self._minutes) + ":" +str(self._seconds) 125 126 if __name__ == '__main__': 127 t = Time(21,31,59) 128 t1 = Time(18,31,41) 129 he = t + t1 130 cha = t - t1 131 print(he) 132 print(cha) 133 print(t==t1) 134 print(t<t1) 135 print(he.hours())