python时间效果 python封装对象实现时间效果

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python时间效果 python封装对象实现时间效果

Dr_W   2021-03-18 我要评论
想了解python封装对象实现时间效果的相关内容吗,Dr_W在本文为您仔细讲解python时间效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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()

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们