• 技术文章 >Python技术 >Python基础教程

    python如何重写父类的方法

    小妮浅浅小妮浅浅2021-06-04 09:36:53原创3904

    1、说明

    关于父类的方法,如果不符合子类模拟的实物行为,可以重写。因此,可以在子类中定义与重写的父类方法同名的方法。

    2、实例

    import threading
    import time
    def catch_fish():
    Pass
    def one_thread():   
     start_time = time.time()    
    for i in range(1, 1001):       
     catch_fish()    
    end_time = time.time()    
    print("单线程测试 耗时 === %s" % str(end_time - start_time))
    def muti_thread():   
     start_time = time.time()   
     for i in range(1, 1001):        
    threading.Thread(target=catch_fish()).start()    
    end_time = time.time()    
    print("多线程测试 耗时 === %s" % str(end_time - start_time))
    if __name__ == '__main__':    
    # 单线程    
    threading.Thread(one_thread()).start()    
    # 多线程    
    muti_thread()

    以上就是python重写父类的方法,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python重写父类
    品易云
    上一篇:python创建新线程有哪些方法 下一篇:Python解释器有几种

    相关文章推荐

    • python Counter的使用• python deque的两大优点• python namedtuple怎样定义一个类• python threading模块有哪些函数• python thread.local()的实例化• python创建新线程有哪些方法

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网