此处init前后有两个“_”
import time
class Clock(object):
"""数字时钟"""
def _init_(self,hour=0,minute=0,second=0): """ 此处init前后有两个“_” """
"""初始化方法"""
self._hour = hour
self._minute = minute
self._second = second
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 '%2d:%2d:%2d'%(self._hour,self._minute,self._second)
def main():
clock = Clock(23,59,58)
while True:
print(clock.show())
time.sleep(1)
clock.run()
if __name__ == '__main__':
main()