zoukankan      html  css  js  c++  java
  • 为什么要重写父类,及怎么重写父类

    1.为什么重写父类?

        所谓重写,就是子类中,有一个和父类相同名字的方法(包括__init__方法),在子类的方法会覆盖掉父类中同名的方法。

    2.如何重写父类?

    (1).

    class Coon(object):
    #基本类
    def __init__(self,host,password,port):
    self.host=host
    self.password = password
    self.port = port

    class CoonMysql(Coon):
    def __init__(self,host,password,port,username,db):
         #username,db是子类用的,不需要传给父类。host,password,port是父类本来就有的,所有传给父类
    Coon.__init__(self,host,password,port)#调用父类的构造方法
    # super(CoonMysql,self).__init__(host,passwd,port)#和上面一句的实现效果一模一样
    #第一句是手动调用父类方法,第二句是super自动找到父类,帮助调用父类方法
    self.username=username #这一条就是子类需要添加的新功能,所以要重写父类
    self.db=db #这一条也是子类需要添加的新功能,所以要重写父类
    def coon_mysql(self):
    self.port

    (2).
    class Consumer(threading.Thread):
    def __init__(self,page_queue,img_queue,*args,**kwargs): #子类新添加了page_queue和img_queue,所以要重写__init__方法
    """(threading父类的__init__方法)
    def __init__(self, group=None, target=None, name=None,
    args=(), kwargs=None, *, daemon=None):
    """
    # 父类中的所有参数都可以用“*args,**kwargs”来代替
    # 重写父类
    super(Consumer,self).__init__(*args, **kwargs)
    self.page_queue = page_queue
    self.img_queue = img_queue
  • 相关阅读:
    dljd_008_jdbc中调用Statement的execute()执行DQL,DDL,DML
    dljd_007_jdbc编程中的statement执行DML/DDL
    【数据结构】可持久化线段树
    【数据结构】可持久化并查集
    【图论】TarjanLCA算法
    【图论】KruskalMST算法
    【基础】标准模板
    【数学】位运算
    【数据结构】Trie
    【数据结构】线段树(名次树)
  • 原文地址:https://www.cnblogs.com/qiaoer1993/p/10489715.html
Copyright © 2011-2022 走看看