计数器不更新值

Counter not updating value

我写了一个程序来打印时间,如下所示:

23:59:59 Sunday

然后我希望它增加到这个:

00:00:00 Monday

相反,日期永远不会更新为新值,并且星期日始终打印在屏幕上。

我从 DayCounter 传递到 CyclicCounter 的数据一定有错误,

或 DayCounter 出错。

    class DayCounter(CyclicCounter):
    _days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
             'Thursday', 'Friday', 'Saturday']
    day = 'Sunday'

    def __init__(self, day):
        self.day = day
        CyclicCounter.__init__(self, len(self._days))
        self.next = day

    def __str__(self):
        return str(self.day)


class DayClock(Clock):

    def __init__(self, h=0, m=0, s=0, day='Sunday'):
        super().__init__(h, m, s)
        self._d = DayCounter(day)

    def __str__(self):
        return Clock.__str__(self) + ' ' + str(self._d)

class Counter:
    def __init__(self, start=0):
        self.value = start

    def advance(self):
        self.value = self.value + 1

    def __str__(self):
        return str(self.value)


class CyclicCounter(Counter):

    def __init__(self, period, start=0):
        self.period = period
        Counter.__init__(self, start)

    def advance(self):
        print("self.value   "  + str(self.value + 1)   + " self.period   " + str(self.period))
        self.value = (self.value + 1) % self.period

    def __str__(self):
        s = Counter.__str__(self)
        return (len(str(self.period - 1)) - len(s)) * '0' + s


class CascadeCounter(CyclicCounter):

    def __init__(self, next, period, start=0):

        CyclicCounter.__init__(self, period, start)
        self.next = next

    def advance(self):
        CyclicCounter.advance(self)
        if self.next and self.value == 0:
            self.next.advance()


class Clock(Counter):
    def __init__(self, h, m, s):
        super().__init__()
        self._h = CyclicCounter(24, h)
        self._m = CascadeCounter(self._h, 60, m)
        self._s = CascadeCounter(self._m, 60, s)

    def advance(self):
        self._s.advance()

    def __str__(self):
        return '{0}:{1}:{2}'.format(self._h, self._m, self._s)



if __name__ == '__main__':
    from time import sleep

    clock = DayClock(23, 59, 59)
    threshold = 5
    while threshold > 0:
        print(str(clock) + "\n")
        sleep(1)
        clock.advance()
        threshold -= 1

问题是 Clock 没有级联到 DayClock

self._h = CyclicCounter(24, h)

应该是

self._h = CascadeCounter(d, 24, h)

为此,您需要引入一个新参数。这样做后,您会注意到其他问题,例如class

class DayCounter(CyclicCounter):

不前进,因为它使用字符串 self.day 而不是递增的 self.value。你可以解决这个问题:

class DayCounter(CyclicCounter):
    _days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
             'Thursday', 'Friday', 'Saturday']

    def __init__(self, day = "Sunday"):
        self.value = DayCounter._days.index(day)
        CyclicCounter.__init__(self, len(self._days))

    def __str__(self):
        return str(DayCounter._days[self.value])

时钟class:

class Clock(Counter):
    def __init__(self, h, m, s, d):
        super().__init__()
        self._h = CascadeCounter(d, 24, h)
        self._m = CascadeCounter(self._h, 60, m)
        self._s = CascadeCounter(self._m, 60, s)

    def advance(self):
        self._s.advance()

    def __str__(self):
        return '{0}:{1}:{2}'.format(self._h, self._m, self._s)

日时钟class:

class DayClock(Clock):
    def __init__(self, h=0, m=0, s=0, day='Sunday'):
        self._d = DayCounter(day)
        super().__init__(h, m, s, self._d)

    def __str__(self):
        return Clock.__str__(self) + ' ' + str(self._d)