java 继承多少级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4733443/
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
How many levels of inheritance
提问by Cornelius
Are there any best practices regarding the maximum depth of the inheritance hierarchy for a framework and guidelines when to use multiple levels of inheritance or join them in a single class?
是否有关于框架的最大继承层次结构深度的最佳实践以及何时使用多级继承或将它们加入单个类的指南?
Some references to established practices/examples that worked well in the Java world would be nice in the answers as this is from the perspective of the community and frameworks surrounding Java.
一些对在 Java 世界中运行良好的既定实践/示例的参考在答案中会很好,因为这是从围绕 Java 的社区和框架的角度来看的。
采纳答案by Andreas Dolk
You may be looking for a nice rule like "not more then 5 levels of inheritance" but I really doubt that there is such a rule. Objects usually model real world entities and the community of all those entities never agreed on a rule (as far as I know)...
您可能正在寻找一个不错的规则,例如“不超过 5 个继承级别”,但我真的怀疑是否存在这样的规则。对象通常模拟现实世界的实体,而所有这些实体的社区从未就规则达成一致(据我所知)......
One "best practice" is definitly: favor composition over inheritance. Following this guideline: define one interface and implementations with no further superclasses. All common fields and methods come with behaviour and strategies
一个“最佳实践”无疑是:支持组合而不是继承。遵循这一准则:定义一个接口和实现,没有进一步的超类。所有常见的领域和方法都带有行为和策略
On the other hand, we sometime model real world entities or structurs that already come with a deep hierarchy of entities. Classification of animalsis one example or algebraic structures. A framework to model such existing structures would require a deep class hierarchy because it should follow the real world just to be understandable.
另一方面,我们有时会对现实世界的实体或已经带有深层实体层次结构的结构进行建模。动物的分类就是一个例子或代数结构。对此类现有结构进行建模的框架将需要很深的类层次结构,因为它应该遵循现实世界才易于理解。
回答by darioo
From what I've seen (note: this isn't an official opinion, just my observations), 3-4 is a reasonable maximum.
从我所看到的(注意:这不是官方意见,只是我的观察),3-4 是合理的最大值。
Also, remember one important part from Effective Java:
另外,请记住 Effective Java 中的一个重要部分:
Favor composition over inheritance
喜欢组合而不是继承
回答by aioobe
In my opinion each derived class is "a class of it's own", and the clients of this class, including subclasses) should in general not bother, or be interested in, how many classes there are between this class and the Object
class.
在我看来,每个派生类都是“它自己的一个类”,这个类的客户,包括子类)一般不应该打扰或感兴趣,这个类和这个Object
类之间有多少个类。
Thus, my answer would be: No, let the hierarchy be as deep as it needs to be, as long as it makes sense / seems logical. Don't let the hierarchy depth influence the decision of whether or not to collapse two classes. Since each subclass is a refinement of it's base class, my experience says that it seldom makes sense to have depths of more than 5 or 6 classes.
因此,我的回答是:不,让层次结构尽可能深,只要它有意义/看起来合乎逻辑。不要让层次深度影响是否折叠两个类的决定。由于每个子类都是其基类的改进,我的经验表明,拥有超过 5 或 6 个类的深度很少有意义。
Just to be clear: As others have pointed out, you should favour composition over inheritance. To me however, that seems to answer a slightly different question though.
明确一点:正如其他人所指出的,您应该更喜欢组合而不是继承。然而,对我来说,这似乎回答了一个稍微不同的问题。
According to this article, the maximum depth in the standard Java API is 9.
根据这篇文章,标准 Java API 中的最大深度为 9。
回答by Paddy
I think, from a maintenance point of view, as shallow an inheritance hierarchy as you can manage is the way to go. Tracing bugs down through multiple hierarchical levels, and then having to figure out at which level they should then be fixed can be a tricky one.
我认为,从维护的角度来看,您可以管理的尽可能浅的继承层次结构是要走的路。通过多个层次级别跟踪错误,然后必须弄清楚应该在哪个级别修复它们可能是一件棘手的事情。
回答by uncaught_exceptions
There is no best practices regarding maximum depth of inheritance. Instead of looking it in volume, this is the rule of thumb when it comes to inheritance in java (and any OO language for that matter).
没有关于最大继承深度的最佳实践。当涉及到 Java 中的继承(以及与此相关的任何 OO 语言)时,这是一条经验法则,而不是从数量上看它。
1_ Before implementing inheritance check if "is a" relationship is true. Charger is a car. (so inheritance is fine). If you do this, then you will not end up with huge inheritance tree anyway. ( Wheel is not a car, so inheritance here is wrong)
1_在实现继承之前检查“是”关系是否为真。充电器是一辆车。(所以继承很好)。如果你这样做,那么无论如何你都不会得到巨大的继承树。(Wheel 不是车,所以这里的继承是错误的)
2_ If "is a" relationship fails or you have ambiguity, then probable inheritance is not the way. You need to identify commonality among your classes and use composition.(refactoring common code to a new class).
2_如果“是一个”关系失败或者你有歧义,那么可能的继承不是办法。您需要确定类之间的共性并使用组合。(将公共代码重构为新类)。
3_ if you want to establish a type hierrarchy, then you should go with Interface inheritance (Type) not class level inheritance. (May be some of these statements need more explanation, but for that you probably need to pick up some book.)
3_如果你想建立一个类型层次结构,那么你应该使用接口继承(Type)而不是类级继承。(可能其中一些陈述需要更多解释,但为此您可能需要拿起一些书。)
回答by Abhed
When it comes to best practices, performance(memory usage) also should be considered.
当谈到最佳实践时,还应该考虑性能(内存使用)。
It will use more memory when you have deep hierarchy of classes (non-abstract). Like creating a new object of last child of 3 level inheritance will use LESS MEMORY than that of 9 level inheritance. Based on nature of application this point could be considered, in addition to other posts.
当您拥有较深的类(非抽象)层次结构时,它将使用更多内存。就像创建一个 3 级继承的最后一个孩子的新对象将比 9 级继承使用更少的内存。除了其他职位之外,还可以根据申请的性质考虑这一点。
回答by Peter Lawrey
You should think first about the clarity of the code you are writing. Will having more level of depth make the code easier to read and maintain or will it make it harder. Alot depending on the problem you are trying to solve.
您应该首先考虑您正在编写的代码的清晰度。拥有更多的深度会使代码更易于阅读和维护,还是会使其变得更难。很大程度上取决于您要解决的问题。
As other posters suggest, you can make the depth either too shallow or too deep. This is a goldilocks problem IMHO.
正如其他海报所建议的那样,您可以使深度太浅或太深。恕我直言,这是一个金发姑娘问题。
回答by detman
The best is that each class takes care of a bunch of similar things.
Each method inside this class takes care of one action/behaviour.
最好的是每个班级都处理一堆相似的事情。
这个类中的每个方法负责一个动作/行为。
Look at this refcard from DZone: http://refcardz.dzone.com/assets/download/refcard/54c22c8b0a53591ea9e0504fdfd4bada/rc130-010d-designing-quality_0.pdf
看看来自 DZone 的这张 refcard:http://refcardz.dzone.com/assets/download/refcard/54c22c8b0a53591ea9e0504fdfd4bada/rc130-010d-designing-quality_0.pdf
回答by Reza
General rule is the deeper the hierarchy the more difficult it might be to understand where particular methods and fields are defined or/and redefined.
一般规则是层次结构越深,就越难理解在哪里定义或/和重新定义了特定的方法和字段。
But beside the inheritance there are other factors that you need to be careful to keep your code complexity manageable.
但是除了继承之外,您还需要注意其他一些因素,以确保代码的复杂性易于管理。
The best approach is to use a code metrics tool and try to balance between different factors(lines of code, class decoupling and ...)
最好的方法是使用代码度量工具并尝试在不同因素(代码行、类解耦和......)之间取得平衡