使用超类“受保护的最终”为子类保留公共代码的方法

发布于 2024-12-08 07:07:53 字数 938 浏览 1 评论 0原文

作为一名(迂腐的)Java 初学者,我想知道,将所有子类使用的公共代码块移动到单独的受保护最终)是否是一个好习惯? ) 父类中的方法?像用通用值填充列表或通用过滤算法等任务...... 也使用受保护的静态方法好吗?

class A {
    protected final List<String> getVariants() {...}
    protected final List<String> filterResults(List<String> variants) {...}
}

class B extends A {
    public List<String> doSomethingUsefull() {
        List<String> commonVariants = getVariants();
        ...
        return filterResults(commonVariants);
    }
}

class C extends A {
    public void doSomethingUsefull() {
        List<String> commonVariants = getVariants();
        ...
        return filterResults(commonVariants);
    }

    public void doMoreUsefullThings() {
        List<String> commonVariants = getVariants();
        ...
        return filterResults(commonVariants);
    }
}

As a (pedantic) beginner Java programmer I would like to know, is it a good practice to move a common block of code that all subclasses use to a separate protected (final) method in parent class? Tasks like filling lists with common values, or common filtering algorithms, etc...
Is it good to also use protected static methods?

class A {
    protected final List<String> getVariants() {...}
    protected final List<String> filterResults(List<String> variants) {...}
}

class B extends A {
    public List<String> doSomethingUsefull() {
        List<String> commonVariants = getVariants();
        ...
        return filterResults(commonVariants);
    }
}

class C extends A {
    public void doSomethingUsefull() {
        List<String> commonVariants = getVariants();
        ...
        return filterResults(commonVariants);
    }

    public void doMoreUsefullThings() {
        List<String> commonVariants = getVariants();
        ...
        return filterResults(commonVariants);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

想挽留 2024-12-15 07:07:53

如果您是 Java 初学者,或者您正在考虑这类事情,那么现在是阅读《Effective Java》一书中的“类和接口”一章的好时机。那里的信息将比您在这里得到的答案更加全面和细致。

以下是考虑混合 finalprotectedstatic 关键字的一种方法:

  • OO 纯粹主义者会建议您避免 static 因为它打破了 OO 范式。
  • 当然,使用final关键字也可以防止子类重写方法。在这方面,结果与static 相同。
  • final 应该更频繁地使用,最好将它与 protected 一起使用。请参阅“Effective Java”中的第 17 条。
  • protectedstatic 不经常一起使用。您会将 OO 构造与破坏正常 OO 行为的构造混合在一起,因此这种组合很奇怪。

If you're a Java beginner, or you are thinking about these sorts of things, then now is a good time to read the "Classes and Interfaces" chapter in a book called "Effective Java." The information there will be more thorough and nuanced than answers that you get here.

Here's one way to think about mixing the final, protected, and static keywords:

  • OO purists will advise you to avoid static because it breaks the OO paradigm.
  • Of course, using the final keyword prevents subclasses from overriding a method as well. In this respect, the outcome is the same as with static.
  • final should be used more often, and it's a good idea to use it along with protected. See Item 17 in "Effective Java".
  • protected and static are not used together very often. You'd be mixing an OO construct with a construct that breaks normal OO behavior, so the combination is odd.
凉城已无爱 2024-12-15 07:07:53

对我来说这似乎是合理的 - 尽管您可能也想使 A 抽象。还可以考虑使用组合 - BC 是否可以包含一个 A 而不是对其进行子类化?

It seems reasonable to me - although you might want to make A abstract as well. Also consider using composition instead - could B and C contain an A instead of subclassing it?

薄荷→糖丶微凉 2024-12-15 07:07:53

首先,您不应该使用扩展来达到此目的,因为过多的扩展总是坏主意。

其次,您不重复代码是完全正确的,但是按代码的重复部分对其进行分组并不是一个好的选择。更好的方法是按照抽象级别对现实世界中有意义的事物进行分组。

最后但并非最不重要的一点是,当您有疑问:是否分开、扩展或组合、受保护的最终或仅受保护时,请尝试为此类编写单元测试,答案会很快得到。

Firstly, you shouldn't use extends to this purpose because too much extending are always bad idea.

Secondly, You're completely right that you don't repeat your code, but grouping it by repeated part of code isn't good choice. Preferable way is to grouping things be meaning in real world, by level of abstraction.

Last but not least, When you have doubts: separately or not, extending or composition, protected final or only protected, try write unit test to this class and answers will come very fast.

牛↙奶布丁 2024-12-15 07:07:53

如果所有这些方法不依赖于某些类字段,我建议将它们移至单独的静态类。让它们成为 util 方法。

I'd propose to move all this methods to separate static class if they don't depend on some class fields. Make them util methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文