1 Star 0 Fork 0

caifan/python_learn

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
object_class05.py 1.11 KB
Copy Edit Raw Blame History
caifan authored 2023-11-24 10:57 +08:00 . 类继承多态
'''
类方法
@classmethod注解在方法上标识
'''
from time import time, localtime, sleep
class Clock(object):
'''数字时钟'''
def __init__(self, hour=0, minute=0, second=0):
self._hour = hour
self._minute = minute
self._second = second
'''类方法'''
@classmethod
def now(cls):
ctime = localtime(time())
return cls(ctime.tm_hour, ctime.tm_min, ctime.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 '%02d:%02d:%02d' % (self._hour, self._minute, self._second)
def main():
# 通过类方法创建对象并获取系统时间
clock = Clock.now()
while True:
# 打印当前时间
print(clock.show())
sleep(1)
clock.run()
if __name__ == '__main__':
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/sharehappy/python_learn.git
git@gitee.com:sharehappy/python_learn.git
sharehappy
python_learn
python_learn
master

Search