鱼C论坛

 找回密码
 立即注册
查看: 2099|回复: 0

[技术交流] 章节十一:简单定制

[复制链接]
发表于 2017-8-23 21:22:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 向西而笑 于 2017-8-24 16:54 编辑

            课时44:简单定制
这节课主要定制一个关于计时器的类
基本要求:
1、 定制一个计时器的类;
2、start和stop方法代表启动计时和停止计时;
3、假设计时器对象t1,print(t1)和直接调用t1均显示结果;
4、当计时器未启动或已经停止计时,调用stop方法会给予温馨的提示;
5、两个计时器对象可以进行相加:t1 + t2;
6、只能使用提供的有限资源完成。

需要的资源:
1、 使用tiem模块的localtime方法获取时间;
2、time.localtime返回struct_time的时间格式
3、表现你的类:__str__和__repr__


1、自己先动手尝试的版本
  1. import time as t

  2. class MyTimer():
  3.         def __init__(self):
  4.                 self.start_time = False  #没有开始计时
  5.                 self.end_time = False  #结束计时不存在
  6.         #开始计时
  7.         def start(self):
  8.                 print('计时开始...')
  9.                 self.start_time = t.time()#返回开始计时的时间戳

  10.         #停止计时
  11.         def stop (self):
  12.                 if   not self.start_time:  #判断计时有没有开始
  13.                        print('【提示】...请开始计时...')
  14.                 else:
  15.                         self.end_time = t.time()  #返回结束计时的时间戳
  16.                         self.total_time = (self.end_time - self.start_time)  #计算总运行秒数
  17.                         print('计时结束...')
  18.                         
  19.         def __str__(self):
  20.                 if  not self.end_time:#判断计时有没有结束
  21.                         return '【提示】...请停止计时...'
  22.                 else:
  23.                         return '运行了%.6f秒'%self.total_time

  24.         __repr__ = __str__  #代码一样就直接赋值
  25.                
  26.         def __add__(self,other):
  27.                 return self.total_time + other.total_time

  28.         def __sub__(self,other):
  29.                 return self.total_time - other.total_time
复制代码

运行结果如下
1.png




2、小甲鱼老师的版本

  1. import time as t

  2. class Mytimer():
  3.         def __init__(self):
  4.                 self.unit = ['年','月','天','小时','分钟','秒']      
  5.                 self.prompt = "尚未开始计时!"  #温馨提示
  6.                 self.lasted = []
  7.                 self.begin = 0
  8.                 self.end = 0
  9.         def __str__(self):
  10.                 return self.prompt

  11.         __repr__ = __str__


  12.         def __add__(self,other):
  13.                 prompt = '总共运行了'
  14.                 result = []
  15.                 for index in range(6):
  16.                         result.append(self.lasted[index] + other.lasted[index])
  17.                         if result[index]:
  18.                                 prompt += (str(result[index]) + self.unit[index])
  19.                 return prompt

  20.         #开始计时
  21.         def start(self):
  22.                 self.begin = t.localtime()
  23.                 self.prompt = '提示,请先调用stop()停止计时!'
  24.                 print('计时开始...')

  25.         #停止计时
  26.         def stop(self):
  27.                 if not self.begin:
  28.                         print('提示:请先调用start()进行计时!')
  29.                 else:
  30.                         self.end = t.localtime()
  31.                         self._calc()
  32.                         print('计时结束!')

  33.         #内部方法,计算运行时间
  34.         def _calc(self):
  35.             self.lasted = []
  36.             self.prompt = '总共运行了'
  37.             for index in range(6):
  38.                 self.lasted.append(self.end[index] - self.begin[index])
  39.                 if self.lasted[index]:
  40.                     self.prompt += (str(self.lasted[index]) + self.unit[index])
  41.             #为下一轮计时初始化变量
  42.             self.begin = 0
  43.             self.end = 0
复制代码


运行结果如下
2.png

评分

参与人数 1鱼币 +5 收起 理由
小甲鱼 + 5

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 18:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表