继承和组合之间的区别
我提取了继承和组合之间的以下区别。我想知道后端对象延迟创建是什么意思?请找出以下差异。
组合允许您延迟后端对象的创建,直到(除非)需要它们,以及在前端对象的整个生命周期中动态更改后端对象。通过继承,一旦子类被创建,您就可以在子类对象映像中获得超类的映像,并且在子类的整个生命周期中它仍然是子类对象的一部分
I have extracted following difference between inheritance and composition. I wanted to know what does delay of creation of back end object mean? Please find below difference.
Composition allows you to delay the creation of back-end objects until (and unless) they are needed, as well as changing the back-end objects dynamically throughout the lifetime of the front-end object. With inheritance, you get the image of the superclass in your subclass object image as soon as the subclass is created, and it remains part of the subclass object throughout the lifetime of the subclass
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在继承中,创建子类时也会创建超类。
在组合中,对象是在编码员想要的时候创建的。
这就是继承,当创建 Child 类时,也会创建父类,因为子类继承自父类。
这就是组合,对象不是在创建子类时创建的,而是在需要时才创建。
在这种情况下,创建 Child 类时也会创建 Parent 类。下面是在创建子类时没有创建对象的组合的另一个示例。
请注意,直到调用 createParent 才创建父类。
In inheritance the superclass is created when the subclass is created.
In Composition, the object is created when the coder wants it to.
This is inheritance, when the Child class is created the parent is created because the child inherits from parent.
This is composition, the object is not created when the child class is created, instead it is created whenever it is need.
In this case, the Parent class is also created when the Child class is created. Below is another example of Composition without the object being created when the child class is created
Note how the parent is not created until a call is made to the createParent.
这意味着,在有人实际调用使用该对象的方法之前,不必创建类封装的对象。
All it means is that the object that your class encapsulates doesn't have to be created until somebody actually calls the method that uses that object.
这也意味着父级的封装没有被破坏。子类化将父类的数据暴露给子类,从而打破了封装性。组合允许对象封装持续存在,并且两个对象可以继续单独管理,这样更改一个类的数据不会影响另一类的数据。
It also means that the encapsulation of the parent is not broken. Subclassing exposes the data of the parent class to the child class, thus breaking the encapsulation. Composition allows the object encapsulation to persist and both objects can continue to be managed separately, so that changing the data of one class does not affect the other class data.
简单来说,组合表示HAS-A关系,而继承表示IS-A关系。
例如,鸡是一种鸟,有喙。所以下面的代码片段展示了它是如何工作的:
In simple words, Composition means HAS-A relationship but Inheritance is an IS-A relationship.
For example, a chicken is a bird and has a beak. So the following code snippet shows how it works: