简体   繁体   English

仅在Override类上调用super方法

[英]Calling only the super method on Override class

I see this code snippet in a Jenkins plugin and I think that it is no-op 我在Jenkins插件中看到了这个代码片段,我认为它是无操作的

  @Override
  public DescriptorImpl getDescriptor() {
    return (DescriptorImpl)super.getDescriptor();
  }

Any reason that this might be essential and cannot be nuked from the code? 有什么理由认为这可能是必不可少的,不能从代码中解脱出来?

During an override, you can modify the return type (if the new return type is a subclass of the old return type). 在覆盖期间,您可以修改返回类型(如果新返回类型是旧返回类型的子类)。
For example : 例如 :

public class A {
  public Number getNumber() { /* ... */ } ;
}

public class B extends A {
  @Override
  public Integer getNumber() { /* ... */ } ;
}

In your sample, super.getDescriptor() may always return a DescriptorImpl object, so you can override the method to avoid some cast in your code. 在您的示例中, super.getDescriptor()可能始终返回DescriptorImpl对象,因此您可以覆盖该方法以避免代码中的某些转换。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM