本文实例为大家分享了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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。