java 使用超类的“protected final”方法来保留子类的通用代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7651741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using superclass "protected final" methods to keep common code for subclasses
提问by dmzkrsk
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 staticmethods?
作为一个(迂腐的)初级 Java 程序员,我想知道,将所有子类使用的公共代码块移动到父类中的单独受保护(final)方法是否是一个好习惯?诸如使用常见值填充列表或常见过滤算法等任务......是否也使用受保护的静态方法?
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);
}
}
回答by jtoberon
If you're a Java beginner, and you are thinking about these sorts of things, then now is a good time to read Chapter 4 "Classes and Interfaces" in a book called "Effective Java." The information there will be more thorough and nuanced than answers that you get here.
如果您是 Java 初学者,并且正在考虑这些事情,那么现在是阅读名为“Effective Java”的书中的第 4 章“类和接口”的好时机。那里的信息将比您在此处获得的答案更全面、更细致。
Here's one way to think about mixing the final
, protected
, and static
keywords:
下面就来想想混合的一种方式final
,protected
和static
关键字:
- 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 withstatic
. final
should be used more often, and it's a good idea to use it along withprotected
. See Item 17 in "Effective Java".protected
andstatic
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.
- OO 纯粹主义者会建议您避免,
static
因为它打破了 OO 范式。 - 当然,使用
final
关键字也可以防止子类覆盖方法。在这方面,结果与 相同static
。 final
应该更频繁地使用,最好将它与protected
. 请参阅“有效的 Java”中的第 17 项。protected
并且static
不经常一起使用。您会将 OO 构造与破坏正常 OO 行为的构造混合在一起,因此这种组合很奇怪。
回答by Jon Skeet
It seems reasonable to me - although you might want to make A
abstract as well. Also consider using composition instead - could B
and C
containan A
instead of subclassing it?
这对我来说似乎是合理的 - 尽管您可能也想A
抽象。还可以考虑使用组合 - 可以B
并C
包含一个A
而不是子类化它吗?
回答by lukastymo
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.
最后但并非最不重要的一点是,当您有疑问时:单独与否,扩展或组合,受保护的最终或仅受保护,请尝试为该课程编写单元测试,答案会很快得到。
回答by Mikita Belahlazau
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.
如果它们不依赖于某些类字段,我建议将所有这些方法移到单独的静态类中。使它们成为实用方法。