python封装对象实现时间效果
作者:Dr_W 时间:2022-10-30 16:14:01
本文实例为大家分享了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,时间
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python将列表中的元素转化为数字并排序的示例
2023-07-06 11:16:11
使用php语句将数据库*.sql文件导入数据库
2023-11-23 05:11:22
使用Python对Dicom文件进行读取与写入的实现
2022-05-30 12:18:27
![](https://img.aspxhome.com/file/2023/4/135304_0s.jpg)
python可视化text()函数使用详解
2023-08-31 19:48:15
![](https://img.aspxhome.com/file/2023/9/107639_0s.png)
用Python爬取英雄联盟的皮肤详细示例
2023-05-07 22:41:19
![](https://img.aspxhome.com/file/2023/7/126997_0s.jpg)
JavaScript正则表达式匹配 div style标签
2024-04-10 10:40:10
pycharm工具连接mysql数据库失败问题
2024-01-22 05:01:27
![](https://img.aspxhome.com/file/2023/8/92068_0s.jpg)
SQL Server中使用SQL语句实现把重复行数据合并为一行并用逗号分隔
2024-01-18 03:06:54
![](https://img.aspxhome.com/file/2023/9/106239_0s.jpg)
javascript应用:Iframe自适应其加载的内容高度
2024-02-25 13:46:07
初学者快看,Python下划线的五个作用介绍
2023-03-03 18:23:40
![](https://img.aspxhome.com/file/2023/7/68297_0s.jpg)
python使用pip安装模块出现ReadTimeoutError: HTTPSConnectionPool的解决方法
2023-07-07 02:41:30
python3模拟百度登录并实现百度贴吧签到示例分享(百度贴吧自动签到)
2023-07-08 23:15:23
javascript 打印内容方法小结
2023-08-22 16:15:09
Python实现进程同步和通信的方法
2023-08-30 04:21:45
Linux中将txt导入到mysql的方法教程
2024-01-12 13:04:27
微信小程序简单的canvas裁剪图片功能详解
2023-08-24 07:49:20
SQL Server中查看对象定义的SQL语句
2024-01-18 05:52:43
详解在node.js中require方法的加载规则
2024-05-08 09:36:10
![](https://img.aspxhome.com/file/2023/7/129467_0s.png)
Python Flask 实现 HTML 文件压缩案例代码(9 级压缩)
2021-05-22 15:22:13
![](https://img.aspxhome.com/file/2023/5/108115_0s.png)
一小时快速入门Python教程
2023-03-06 04:07:03