python封装对象实现时间效果
作者:Dr_W
这篇文章主要为大家详细介绍了python封装对象实现时间效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- python操作日期和时间的方法
- Python时间戳与时间字符串互相转换实例代码
- python利用datetime模块计算时间差
- 10种检测Python程序运行时间、CPU和内存占用的方法
- python中日期和时间格式化输出的方法小结
- Python中实现对Timestamp和Datetime及UTC时间之间的转换
- Python中datetime常用时间处理方法
- python中关于时间和日期函数的常用计算总结(time和datatime)
- python简单实现获取当前时间
- Python之日期与时间处理模块(date和datetime)
- Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
- Python获取当前时间的方法
- python获取当前时间对应unix时间戳的方法
- python获得文件创建时间和修改时间的方法
- Python比较2个时间大小的实现方法
- Python3时间转换之时间戳转换为指定格式的日期方法详解
