import time from os import system # 定义始终类 class clock: # 对象属性(初始化方法) def __init__(self, hour=0, minute=0, second=0): self.hour = hour self.min = minute self.sec = second # 对象方法 # 时间流逝 def run(self): self.sec += 1 if self.sec == 60: self.min += 1 self.sec = 0 if self.min == 60: self.hour += 1 self.min = 0 if self.hour == 24: self.hour = 0 # 时间显示 def show(self): # 如果长度不足2,使用0填充,在左侧填充 return f'{self.hour:0>2d}:{self.min:0>2d}:{self.sec:0>2d}' # 创建对象(实例化) c1 = clock(19, 49, 20) while True: print(c1.show()) c1.run() # 休眠一秒 time.sleep(1) # 调用linux清屏命令 system('cls') 运行结果: