鱼C论坛

 找回密码
 立即注册
查看: 2626|回复: 10

[已解决]计时器add函数,不太会写,大家会的给点思路

[复制链接]
发表于 2018-11-6 14:46:28 From FishC Mobile | 显示全部楼层 |阅读模式

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

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

x
这是我自己写的计时器
但是add函数不会写
哪位大神给点思路
新手上路,谢谢了

import time as t

class Mytimer():
    def __init__(self):
        self.duringtime='未开始计时'
        self.starttime=0
   
    def __repr__(self):
        return(self.duringtime)

    def __add__(self,other):

   
    #计时开始
    def start(self):
        self.starttime=t.time()
        print('计时开始')
        self.duringtime='未结束计时'
        
    #计时结束
    def stop(self):
        if not self.starttime:
            print('未开始计时')
        else:
            self.stoptime=t.time()
            print('计时结束')
            self.count()
        
    #计算时间间隔
    def count(self):
        self.duringtime='运行了'+str(int(self.stoptime-self.starttime))+'s'
最佳答案
2018-11-6 14:56:28
你定义一个参数self.last_time用于存放持续时间,并初始为0,
stop时候计算下
这样add就返回self.last_time加other.last_time就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-11-6 14:56:28 | 显示全部楼层    本楼为最佳答案   
你定义一个参数self.last_time用于存放持续时间,并初始为0,
stop时候计算下
这样add就返回self.last_time加other.last_time就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-6 18:51:04 | 显示全部楼层
本帖最后由 BigSmall 于 2018-11-6 18:52 编辑
塔利班 发表于 2018-11-6 14:56
你定义一个参数self.last_time用于存放持续时间,并初始为0,
stop时候计算下
这样add就返回self.last_ti ...


import time as t

class Mytimer():
    def __init__(self):
        self.duringtime='未开始计时'
        self.starttime=0
        self.lasttime=0
   
    def __repr__(self):
        return(self.duringtime)

    def __add__(self,other):
        self.duringtime='一共运行了'+str(int(self.lasttime+other.lasttime))+'s'
   
    #计时开始
    def start(self):
        self.starttime=t.time()
        print('计时开始')
        self.duringtime='未结束计时'
        
    #计时结束
    def stop(self):
        if not self.starttime:
            print('未开始计时')
        else:
            self.stoptime=t.time()
            print('计时结束')
            self.count()
        
    #计算时间间隔
    def count(self):
        self.lasttime=self.stoptime-self.starttime
        self.duringtime='运行了'+str(int(self.lasttime))+'s'
        
        

我是不是理解错了,运行失败,麻烦你你再帮忙看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-6 18:56:41 | 显示全部楼层
把你失败消息贴出来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-6 19:02:31 | 显示全部楼层
塔利班 发表于 2018-11-6 18:56
把你失败消息贴出来

>>> t1=Mytimer()
>>> t1.start()
计时开始
>>> t1.stop()
计时结束
>>> t1
运行了5s
>>> t2=Mytimer()
>>> t2.start()
计时开始
>>> t2.stop()
计时结束
>>> t2
运行了6s
>>> t1+t2
>>> type(t1+t2)
<class 'NoneType'>
>>> print(t1+t2)
None
>>>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-6 19:03:02 | 显示全部楼层
塔利班 发表于 2018-11-6 18:56
把你失败消息贴出来

t1+t2没有实现
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-6 19:19:39 | 显示全部楼层

你函数没加return
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-6 19:24:56 | 显示全部楼层

谢谢,很强
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-6 21:38:08 | 显示全部楼层

我弱弱的问一句啊,加上return可以实现add,但我想问为什么必须有return?谢谢大神
之前我这个__add__函数没有return,我是想的,直接用repr打印出来,可是实现不了。
def __repr__(self):
        return(self.duringtime)
def __add__(self,other):
        self.duringtime='一共运行了'+str(int(self.lasttime+other.lasttime))+'s'

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-6 21:41:06 | 显示全部楼层

我弱弱的问一句啊,加上return可以实现add,但我想问为什么必须有return?谢谢大神
之前我这个__add__函数没有return,我是想的,直接用repr打印出来,可是实现不了。
def __repr__(self):
        return(self.duringtime)
def __add__(self,other):
        self.duringtime='一共运行了'+str(int(self.lasttime+other.lasttime))+'s'
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-6 21:42:29 | 显示全部楼层
add没必要打出来,因为不是属于实例自身的,是和另一个实例一起作用的结果
其实我觉得加一起的意义也不是很大
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 22:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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