想了解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()