设计一个名为stopwatch的秒表类
时间: 2024-06-03 12:08:33 浏览: 82
class Stopwatch:
def __init__(self):
self.__start_time = None
self.__elapsed_time = 0
def start(self):
self.__start_time = time.time()
def stop(self):
if self.__start_time is not None:
elapsed_time = time.time() - self.__start_time
self.__elapsed_time += elapsed_time
self.__start_time = None
def reset(self):
self.__start_time = None
self.__elapsed_time = 0
def elapsed_time(self):
if self.__start_time is not None:
elapsed_time = time.time() - self.__start_time
return self.__elapsed_time + elapsed_time
else:
return self.__elapsed_time
阅读全文