当前位置 博文首页 > python封装对象实现时间效果

    python封装对象实现时间效果

    作者:Dr_W 时间:2021-02-10 21:30

    本文实例为大家分享了python封装对象实现时间效果的具体代码,供大家参考,具体内容如下

    # 钟表
    import time
    class Clock():
      def __init__(self, hour, minute, second):  # 时 分 秒
        self.hour = hour
        self.minute = minute
        self.second = second
      @classmethod
      def now(cls):
        nowtime = time.localtime()
        return cls(nowtime.tm_hour, nowtime.tm_min, nowtime.tm_sec)
      def run(self):
        self.second += 1
        if self.second == 60:
          self.second = 0
          self.minute += 1
          if self.minute == 60:
            self.minute = 0
            self.hour += 1
            if self.hour == 24:
              self.hour = 0
      def show(self):
        return "{} : {} : {}".format(self.hour, self.minute, self.second)
    
    if __name__ == '__main__':
        cl = Clock.now()
        while True:
          print(cl.show())
          time.sleep(1)
          cl.run()
    js
    下一篇:没有了