英文:
How to make choice between inheritance and composition?
问题
我正在讨论继承和组合之间的区别。每个人都赞成组合优于继承,所以问题是我们如何做出选择?另外,如果我们根本不使用继承,那么根据面向对象编程原则,我们会错过什么?
英文:
I was going through the difference between inheritance and composition. Everyone has praised composition over inheritance, so questions is how do we make choice ? Also if we didn't use inheritance at all then what are we going to miss as per OOP principal ?
答案1
得分: 1
代码部分不要翻译,只返回翻译好的内容:
这实际上取决于你的数据结构。继承用于“是一个”关系,而组合用于“有一个”关系。
例如,如果你看汽车。
继承:在“汽车”下面会有“Toyota”、“Honda”、“BMW”,因为它们是汽车的类型。
组合:在“汽车”下面会有“引擎”、“车轮”、“车门”,因为它们是构成汽车的部分。
“Toyota”、“Honda”、“BMW”不是汽车的部分,就像“引擎”、“车轮”、“车门”不是汽车的类型。
英文:
It really comes down to how your data is structured. Inheritance is used for "is a" relationships while composition is used for "has a" relationships.
For example, if you were look at cars.
Inheritance: you would have 'Toyota', 'Honda', 'BMW' under 'Car' because they are types of cars.
Composition: you would have 'Engine', 'Wheels', 'Door' under 'Car' because they are parts that make up the car.
'Toyota', 'Honda', 'BMW' aren't parts of a car same way 'Engine', 'Wheels', 'Door' aren't types of cars.
答案2
得分: 1
这是一个复杂的大主题,也许在一些stackoverflow的帖子中无法进行详尽描述。继承可能会很棘手,有些人在此上面写了博士论文。大多数情况下,当我编写新代码时,我会尽量减少使用继承。
当你的Dog类继承Mammal类时,所有Mammal类的状态/行为仍然适用(狗 是 哺乳动物)。鸭嘴兽是哺乳动物吗?从生物学角度来看是,但这可能不适用于你的需求。特别是当你有可变对象时,会遇到混乱的情况,比如 https://en.wikipedia.org/wiki/Circle%E2%80%93ellipse_problem#Description
另外,在现实生活中,父类和子类之间经常存在一些滑稽的耦合关系。
英文:
This is a big complicated topic, perhaps too much to be described in a few SO posts.
Inheritance can be tricky, People have done Ph.D dissertations on this. Most of the time when I write new code I keep inheritance to a minimum.
When your Dog class inherits Mammal all the state/behavior of Mammal must still apply.
(the dog is a mammal). Is a platypus a mammal? Biology-yes, but it may not apply to your needs. Especially when you have mutable objects you run into
messy situations such as https://en.wikipedia.org/wiki/Circle%E2%80%93ellipse_problem#Description
Also in real life there's often goofy couplings between parent and child classes.
答案3
得分: 1
你不能说一个应该优先于另一个。这取决于数据的结构方式以及特定数据模型所需的表示。有一个简单的方法来记住什么时候使用:
继承 = 是
- 一个对象从另一个对象继承的事实意味着从层次上看,该对象实际上就是它继承的东西。让我们从生物学中的简化 分类法 中举一个例子。在处理 界 动物界 的时候,我们可以说负鼠是哺乳动物,哺乳动物是脊椎动物,脊椎动物是动物。因此,在这里我们有一个继承链:
Opossum extends Mammal
,Mammal extends Chordate
,Chordate extends Animal
。每个群组都有各种特征,所有子类型都会继承这些特征。
组合 = 有
-
组合关系是 有。然而,在这里,这种关系应该被理解为由...组成或包含关系。如果我们仍然处于生物学的世界中,我们可以说负鼠有头、尾巴、身体和4条腿,因此这些对象之间存在组合关系。在Java中,这可以用实例字段表示:
class Opossum { Head head; Body body; Leg[] legs = new Leg[4]; }
英文:
You cannot say that one should be preferred over another. It depends how the data is structured and what representation is required for a particular data model. There is a simple way to remember what to use:
Inheritance = is
- The fact an object inherits from another means the object actually is what it inherits from, hierarchically. Let's take an example from the simplified taxonomy in biology. Working with the kingdom Animalia, we can say that Opossum is a Mammal, which is Chordate, which is animal. Therefor here we have a chain of the inheritance:
Opossum extends Mammal
,Mammal extends Chordate
,Chordate extends Animal
. Each of the group has various characteristics that all the subtypes inherit.
Composition = has
-
The composition is has. However, here this relation should be rather understood as is composed from or contains. If we stay in the world of biology, we can say an Opossum has a head, tail, body and 4 legs, therefore there is a composition relationship between these objects. This is in Java represented as instance fields:
class Opossum { Head head; Body body; Leg[] legs = new Legs[4]; }
答案4
得分: 0
你可以谷歌成千上万篇关于它的文章。所以,如果你想要更深入地了解,谷歌并学习,我们没有必要在这里重新发明轮子。
如果你想要对“组合优于继承”这个概念有一个简短的解释,它基本上是在说 - 不要用一些超级复杂的继承来过度设计你的解决方案。如果你开发了什么东西,你看到了一些明显的继承优势,并且用组合无法实现相同的效果,你可以自由地使用继承。如果你没有看到这一点,或者你可以用组合实现相同的效果,就使用组合。
英文:
You can google thousands articles about it. So if you want to understand more deeply, google and learn, we dont have to reinvent wheel here.
If you want short explanation what composition over inheritance
means, it basically says - do not overengineer your solution with some super complex inheritance. If you develop something and you see some clear advantages of inheritance and you cant achieve the same with composition, you are free to use inheritance. If you dont see it or you can achieve the same with composition, use composition.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论