用python建立一个时间类Time, 它包含时 (hour)、分(minute)、秒(second)的实例 属性。 ① 设计时间显示函数 show(self。 ② 设计两个时间大小比较函数 compare(selfD,其中七是另外一个时间。
时间: 2024-02-28 14:53:10 浏览: 149
好的,下面是Python代码实现:
```python
class Time:
def __init__(self, hour, minute, second):
self.hour = hour
self.minute = minute
self.second = second
def show(self):
print(f"{self.hour}:{self.minute}:{self.second}")
def compare(self, other):
if self.hour > other.hour:
return 1
elif self.hour < other.hour:
return -1
else:
if self.minute > other.minute:
return 1
elif self.minute < other.minute:
return -1
else:
if self.second > other.second:
return 1
elif self.second < other.second:
return -1
else:
return 0
```
其中,`__init__`是初始化方法,用于创建对象时设置属性值;`show`方法用于显示时间;`compare`方法用于比较两个时间的大小,返回值为1表示当前时间较大,返回值为-1表示当前时间较小,返回值为0表示两个时间相等。
阅读全文
相关推荐















