我在正确执行以下内容时遇到问题:
Remove
__hours
and__minutes
. Adjust the implementation of the mutator and accessor methods. For example, for__init__(hr, min, sec)
, validate the values, then set__seconds = hr * 60 * 60 + min * 60 + sec
. This will store the time as seconds. Adjust all the methods to work with__seconds
.getSecond()
can use__seconds
mod 60 to return only the seconds in the time. Test all the methods to make sure they still work. (mod is modulus, the remainder after a division.)
我在我的代码中使用了这个self.setSecond = (hour * 60 * 60) + (minute * 60 + second)
为了正确地表示时、分、秒到秒,我在实现程序的其余部分时遇到了问题。我不确定我是否应该打印几秒钟?另外,当我将getSecond()函数更改为返回% 60
时,它并没有这样做。我这么想是因为我没有恰当地称呼它?你知道吗
以下是迄今为止我的代码:
class Clock(object):
def __init__(self, hour, minute, second):
self.setHour(hour)
self.setMinute(minute)
self.setSecond = (hour * 60 * 60) + (minute * 60 + second)
def setHour(self, hour):
self.__hour = hour
if self.__hour > 23:
self.__hour = 0
elif self.__hour < 0:
self.__hour = 0
def getHour(self):
return self.__hour
def setMinute(self, minute):
self.__minute = minute
if self.__minute > 60:
self.__minute = 0
elif self.__minute < 0:
self.__minute = 0
def getMinute(self):
return self.__minute
def setSecond(self, second):
self.__second = second
if self.__second > 60:
self.__second = 0
elif self.__second < 0:
self.__second = 0
def getSecond(self):
return self.__second
def __str__(self):
if self.__hour > 11:
return 'The Time is {}:{}:{} PM'.format(self.__hour, self.__minute, self.__second)
else:
return 'The Time is {}:{}:{} AM'.format(self.__hour, self.__minute, self.setSecond)
stopwatch = Clock(3, 2, 1)
print(stopwatch)
注意:我知道这个代码不是很pythonic,但这就是我被告知要保留它的方式(我道歉)。你知道吗
主要需要6个更改:
你不需要在课堂上保持时间和分钟。你只维持秒数。所以删除
setHour
和setMinute
方法。因为您不维护小时和分钟,
getHour
和getMinute
方法应该执行所需的计算。你知道吗在
init
函数中,调用自我设定秒不正确。应该是:尽管您提到过,但是您的
getSecond()
方法没有执行seconds % 60
。应该是:在方法
def __str__
中,不要直接访问小时、分钟、秒,而是使用访问器方法:setSeconds
方法不应检查> 60
,因为现在将在此处存储大量数据:相关问题 更多 >
编程相关推荐