调整类内的方法

2024-09-29 17:17:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在正确执行以下内容时遇到问题:

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,但这就是我被告知要保留它的方式(我道歉)。你知道吗


Tags: theto代码selfreturnifisdef
1条回答
网友
1楼 · 发布于 2024-09-29 17:17:34

主要需要6个更改:

  1. 你不需要在课堂上保持时间和分钟。你只维持秒数。所以删除setHoursetMinute方法。

  2. 因为您不维护小时和分钟,getHourgetMinute方法应该执行所需的计算。你知道吗

    def getHour(self):
        return int(self.__second / 3600)
    
    def getMinute(self):
        return int(self.__second / 60) % 60
    
  3. init函数中,调用自我设定秒不正确。应该是:

    def __init__(self, hour, minute, second):
        if hour > 23 or hour < 0:
            hour = 0
        if minute > 60 or minute < 0:
            minute = 0
        if second > 60 or second < 0:
            second = 0
        self.setSecond((hour * 60 * 60) + (minute * 60 + second))
    
  4. 尽管您提到过,但是您的getSecond()方法没有执行seconds % 60。应该是:

    def getSecond(self):
        return self.__second % 60
    
  5. 在方法def __str__中,不要直接访问小时、分钟、秒,而是使用访问器方法:

    def __str__(self):
        if self.getHour() > 11:
            return 'The Time is {}:{}:{} PM'.format(self.getHour(), self.getMinute(), self.getSecond())
        else:
            return 'The Time is {}:{}:{} AM'.format(self.getHour(), self.getMinute(), self.getSecond())
    
  6. setSeconds方法不应检查> 60,因为现在将在此处存储大量数据:

    def setSecond(self, second):
        self.__second = second
        if self.__second < 0:
            self.__second = 0
    

相关问题 更多 >

    热门问题