使用超类“受保护的最终”为子类保留公共代码的方法
作为一名(迂腐的)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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您是 Java 初学者,或者您正在考虑这类事情,那么现在是阅读《Effective Java》一书中的“类和接口”一章的好时机。那里的信息将比您在这里得到的答案更加全面和细致。
以下是考虑混合
final
、protected
和static
关键字的一种方法:static 因为它打破了 OO 范式。
final
关键字也可以防止子类重写方法。在这方面,结果与static
相同。final
应该更频繁地使用,最好将它与protected
一起使用。请参阅“Effective Java”中的第 17 条。protected
和static
不经常一起使用。您会将 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
, andstatic
keywords:static
because it breaks the OO paradigm.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.对我来说这似乎是合理的 - 尽管您可能也想使
A
抽象。还可以考虑使用组合 -B
和C
是否可以包含一个A
而不是对其进行子类化?It seems reasonable to me - although you might want to make
A
abstract as well. Also consider using composition instead - couldB
andC
contain anA
instead of subclassing it?首先,您不应该使用扩展来达到此目的,因为过多的扩展总是坏主意。
其次,您不重复代码是完全正确的,但是按代码的重复部分对其进行分组并不是一个好的选择。更好的方法是按照抽象级别对现实世界中有意义的事物进行分组。
最后但并非最不重要的一点是,当您有疑问:是否分开、扩展或组合、受保护的最终或仅受保护时,请尝试为此类编写单元测试,答案会很快得到。
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.
如果所有这些方法不依赖于某些类字段,我建议将它们移至单独的静态类。让它们成为 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.