简体   繁体   English

为什么子类会覆盖基类方法,但使其完全相同(而不使用它)?

[英]Why a subclass would override a base class method but make it identical (and not use it)?

Studying inheritance in Python, and my understanding so far is that a subclass only overwrites a method of the base class if it is intends for the method to do something different that of the base class. 在Python中研究继承性,到目前为止,我的理解是,如果子类打算让该方法执行与该基类不同的操作,则该子类仅会覆盖该方法。

So using HowDoI as an example, we see that in test_howdoi.py the HowdoiTestCase class, which inherits from unittest.TestCase , overwrites TestCase 's setUp() function (which just pass es): 因此,以HowDoI为例,我们在test_howdoi.py中看到了继承自unittest.TestCaseHowdoiTestCase类,它覆盖了TestCasesetUp()函数(仅pass es):

   def setUp(self):                                          
       self.queries = ['format date bash',                   
                       'print stack trace python',           
                       'convert mp4 to animated gif',        
                       'create tar archive']                 
       self.pt_queries = ['abrir arquivo em python',         
                          'enviar email em django',          
                          'hello world em c']                
       self.bad_queries = ['moe',                            
                           'mel'] 

OK so far. 到目前为止还可以。

test_howdoi.py then goes on though to overwrite tearDown() , yet it is written to just pass (as per the base class's definition). 然后,test_howdoi.py继续覆盖tearDown() ,但是它被写入为仅pass (根据基类的定义)。 And tearDown() does not get used anywhere. 而且tearDown()不会在任何地方使用。

  • Why would a base class function be overwritten with the same behaviour as its behavior in the base class? 为什么用与基类中相同的行为覆盖基类函数?
  • Why would it be overwritten at all if there is no intention to use it? 如果无意使用它,为什么将其全部覆盖?

Structure you're describing may be reduced to following code: 您正在描述的结构可以简化为以下代码:

class A(object):
    def f(self):
        pass

class B(A):
    # this declaration is redundant since exact same behaviour is already inherited.
    def f(self):  
        pass

And trying to answer your question: 并尝试回答您的问题:

Why would it be overwritten at all if there is no intention to use it? 如果无意使用它,为什么将其全部覆盖?

setUp and tearDown methods ARE used in unittest runners (as name suggests - before and after test). setUptearDown方法用于单元测试运行程序中(顾名思义-测试前后)。

Why would a base class function be overwritten with the same behaviour as its behavior in the base class? 为什么用与基类中相同的行为覆盖基类函数?

Some of reasons may be: 某些原因可能是:

  • IDEs generated TestCase class stub, developer just kept it IDE生成了TestCase类存根,开发人员只是保留了它
  • Developer didn't read docs, which states that setUp and tearDown are optional and have empty implementations by default 开发人员未阅读文档,该文档指出setUptearDown是可选的,默认情况下具有空的实现
  • Some developers like to explicitly write that setUp / tearDown is empty for current test suite 一些开发人员喜欢明确地写出setUp / tearDown对于当前测试套件为空

TLDR: By accident or due to personal preference. TLDR:偶然或由于个人喜好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python属性和方法覆盖问题:为什么子类属性仍然调用基类的方法 - Python property and method override issue: why subclass property still calls the base class's method Python:为什么将子类方法的 **kwarg 作为参数传递到基础 class 的 __call__() 签名中? 结果类型错误 - Python: Why would a **kwarg of a subclass method be passed as a parameter in __call__() signature of the base class? Results in TypeError 如何在派生类的子类中使用基类的方法? - How to use the method of base class in the subclass of derived class? 如何覆盖子类中基类的静态抽象方法? - How to override static abstract method of base in a subclass? 基于子类的 Base class 方法的类型提示 - Type hint of Base class method based on Subclass Python 子类覆盖了具有不同签名的方法,因此不会调用基类方法。 这是为什么? - Python subclass overridden method with different signature, so base class method is not called. Why is that? 我的代码无法覆盖基类中的方法 - cant override method in a base class with my code 覆盖没有基础 class 看到它的方法 - Override a method without base class seeing it 通过基类检索时,方法重写失败 - method override fails when retrieving by base class 是否可以继承不可变日期 class 并覆盖 __str__ 方法? - Is it possible to subclass the immutable date class and override the __str__ method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM