Java代码中的聚合和组合 [英] Aggregation and Composition in Java Code

查看:168
本文介绍了Java代码中的聚合和组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于聚合和组合是关联协会,或者我们可以说它可以理解对象或其他任何东西之间的关系。

As the Aggregation and Composition is related Association or we can say it gives the understanding of relationship between object or anything else.

我发布了这个问题,因为我问过一个在采访中的问题是什么是构成和聚合。

I posted this question because i asked one question in Interview that what is the Composition and Aggregation.

根据我的理解,我给出了我的想法,如下所示。

So as per my understanding i given my idea which is as follow.

http://www.coderanch.com / t / 522414 / java / java / Association-Aggregation-Composition

聚合,关联和组合

Java中的关联与聚合与组合

还访问了更多。

和我的基本解释离子是相关的,聚合表示松散的关系,而组成表明强关系和明确的解释与此相关。

and my basic explanation was related that Aggregation indicates loose relationship while Composition indicates Strong relationship and clear explanation related to this.

但是访问者侮辱我并说这是理论概念关于你说我想要完美的Java代码,它们在哪些方面有所不同,并告诉我是否会给你一个小的应用程序,那么你如何确定这是聚合,这是组合?

But the Interviewer Insulted me and said this is the theoretical concept about which you saying i want the perfect Java code in which how they differ and also told if i will give you one small Application then how would you identify that this is the Aggregation and this is the Composition?

所以现在我想了解纯技术概念和Java代码,它们有何不同之处以及它们是什么?

So now i want to understand pure Technical Concept and Java code in which how they differ and what it is?

推荐答案

考虑一个 LinkedList 类的简单示例。 LinkedList 节点组成。这里 LinkedList 类是拥有对象。当它被销毁时,其中包含的所有节点都将被删除。如果从列表中删除 Node 对象,则 LinkedList 对象仍然存在。

Consider a simple example of a LinkedList class. A LinkedList is made up of Nodes. Here LinkedList class is the owning object. When it is destroyed, all the Nodes contained in it are wiped off. If a Node object is deleted from the list, the LinkedList object will still exist.

实现组合使得对象包含另一个对象。

Composition is implemented such that an object contains another object.

class LinkedList {

 Node head;
 int size; 
}

这是作文。

另一个例子,考虑一个 Circle 类,它有一个 Point 类,用于定义其坐标中央。如果 Circle 被删除,其中心将被删除。

Another example, consider a Circle class which has a Point class which is used to define the coordinates of its center. If the Circle is deleted its center is deleted along with it.

class Circle {

    private float radius;
    private Point center;

    public Circle(Point center, float radius) {
        this.center = center;
        this.radius = radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public void setCenter(Point center) {
        this.center = center;
    }

    public float getRadius() {
        return radius;
    }

    public Point getCenter() {
        return center;
    }

    class Point {
        private int x;
        private int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;

        }

        public void setY(int y) {
            this.y = y;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public int getX() {
            return x;
        }
    }

    @Override
    public String toString() {

        return "Center: " + "(" + center.getX() + "," + center.getY() + ")" + "\nRadius: " + this.getRadius()
                + " centimeters";
    }

}

来自java集合api的组合示例: HashMap 有一个 Entry 类。如果删除 HashMap 对象,则会删除地图中包含的所有 Entry 对象。

Example of composition from java collections api : HashMap has an Entry class. If the HashMap object is deleted all the Entry objects contained in the map are deleted along with it.

聚合也是一种对象组合但不如作文强。它定义了HAS A关系。如果A类具有B类的引用并且删除B不会影响A的存在则使用非常简单的单词,那么它就是聚合。你必须在某个时候使用它。

Aggregation is also a type of Object Composition but not as strong as Composition. It defines "HAS A" relationship. Put in very simple words if Class A has a reference of Class B and removing B doesn't affect the existence of A then it is aggregation. You must have used it at some point.

这篇关于Java代码中的聚合和组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆