python怎样实现封装_python封装对象实现时间效果
# 鐘表import timeclass 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封装对象实现时间效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java赋值语句_Scala守卫语句的集
- 下一篇: 选中的磁盘具有mbr分区表。在efi_如