继承和组合之间的区别

发布于 2024-09-08 00:34:57 字数 202 浏览 2 评论 0原文

我提取了继承和组合之间的以下区别。我想知道后端对象延迟创建是什么意思?请找出以下差异。

组合允许您延迟后端对象的创建,直到(除非)需要它们,以及在前端对象的整个生命周期中动态更改后端对象。通过继承,一旦子类被创建,您就可以在子类对象映像中获得超类的映像,并且在子类的整个生命周期中它仍然是子类对象的一部分

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不忘初心 2024-09-15 00:34:57

在继承中,创建子类时也会创建超类。
在组合中,对象是在编码员想要的时候创建的。

这就是继承,当创建 Child 类时,也会创建父类,因为子类继承自父类。

class Parent {

    //Some code
}

class Child extends Parent{

    //Some code
}

这就是组合,对象不是在创建子类时创建的,而是在需要时才创建。

class Parent{

    //Some code
}

class Child{

    private Parent parent = new Parent();
    //Some code
}

在这种情况下,创建 Child 类时也会创建 Parent 类。下面是在创建子类时没有创建对象的组合的另一个示例。

class Parent{

    //Some code
}

class Child{

    private Parent parent;

    public Child()
    {
    }
    public void createParent()
    {
         parent = new 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.

class Parent {

    //Some code
}

class Child extends Parent{

    //Some code
}

This is composition, the object is not created when the child class is created, instead it is created whenever it is need.

class Parent{

    //Some code
}

class Child{

    private Parent parent = new Parent();
    //Some code
}

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

class Parent{

    //Some code
}

class Child{

    private Parent parent;

    public Child()
    {
    }
    public void createParent()
    {
         parent = new Parent();
    }
}

Note how the parent is not created until a call is made to the createParent.

递刀给你 2024-09-15 00:34:57

这意味着,在有人实际调用使用该对象的方法之前,不必创建类封装的对象。

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.

内心激荡 2024-09-15 00:34:57

这也意味着父级的封装没有被破坏。子类化将父类的数据暴露给子类,从而打破了封装性。组合允许对象封装持续存在,并且两个对象可以继续单独管理,这样更改一个类的数据不会影响另一类的数据。

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.

鹿港巷口少年归 2024-09-15 00:34:57

简单来说,组合表示HAS-A关系,而继承表示IS-A关系。

例如,鸡是一种鸟,有喙。所以下面的代码片段展示了它是如何工作的:

/*
* Parent class for Chicken.
*/
class Bird {
    ...
    //code
}  

class Beak {
    ...
    //code
} 

/* 
* As Chicken is a bird so it extends the Bird class. 
*/
class Chicken extends Bird { 

      private Beak beak; // Chicken has a beak so, Chicken class has an instance of Beak class. 
}

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:

/*
* Parent class for Chicken.
*/
class Bird {
    ...
    //code
}  

class Beak {
    ...
    //code
} 

/* 
* As Chicken is a bird so it extends the Bird class. 
*/
class Chicken extends Bird { 

      private Beak beak; // Chicken has a beak so, Chicken class has an instance of Beak class. 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文