checkstyle方法不是为扩展而设计的-必须为抽象,最终或空 [英] checkstyle Method is not designed for extension - needs to be abstract, final or empty

查看:166
本文介绍了checkstyle方法不是为扩展而设计的-必须为抽象,最终或空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口

public interface LdapConnectionFactory {
    LdapConnectionWrapper getConnectionWrapper() throws LDAPException;
}

和实现类 LdapConnectionFactoryImpl 实现 getConnectionWrapper()方法。

public class LdapConnectionFactoryImpl implements LdapConnectionFactory {
    ...
    public LdapConnectionWrapper getConnectionWrapper() throws LDAPException {
        ...
    }
}

当我运行checkstyle时,它会将 getConnectionWrapper()标记为错误-方法 getConnectionWrapper为不是为扩展而设计的-必须是抽象的,最终的或空的。
有什么建议吗?谢谢。

When I ran checkstyle, it flags the getConnectionWrapper() as error - Method 'getConnectionWrapper' is not designed for extension - needs to be abstract, final or empty. Any advice? Thanks.

推荐答案

Checkstyle抛出此消息是因为您提供了实现,但尚未将方法标记为final。您可以通过Eclipse首选项和编辑Checkstyle配置禁用此检查。具体规则是类设计>扩展设计。

Checkstyle is throwing this message because you have provided an implementation but have not marked the method as final. You can disable this check via Eclipse Preferences and editing your Checkstyle configuration. The specific rule is Class Design > Design for Extension.

支票的说明相当不错,如下所示:

The description for the check is pretty decent, and is as follows:


检查类是否为扩展设计。更具体地说,它强制执行一种编程样式,其中超类提供可以由子类实现的空挂钩。
确切的规则是,可以子类化的类的非私有,非静态方法必须为

Checks that classes are designed for extension. More specifically, it enforces a programming style where superclasses provide empty "hooks" that can be implemented by subclasses. The exact rule is that nonprivate, nonstatic methods of classes that can be subclassed must either be


  • 抽象或

  • final或

  • 有一个空的实现

理论上: API设计风格可以保护超类免受子类破坏。缺点是子类的灵活性受到限制,特别是它们无法阻止超类中代码的执行,但这也意味着子类无法通过忘记调用super方法来破坏超类的状态。

Rationale: This API design style protects superclasses against beeing broken by subclasses. The downside is that subclasses are limited in their flexibility, in particular they cannot prevent execution of code in the superclass, but that also means that subclasses cannot corrupt the state of the superclass by forgetting to call the super method.

这篇关于checkstyle方法不是为扩展而设计的-必须为抽象,最终或空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆