java合成关系和聚合关系_Java中的聚合(HAS-A关系)

java合成关系和聚合关系

Aggregation is a term which is used to refer one way relationship between two objects. For example, Student class can have reference of Address class but vice versa does not make sense.

聚合是一个术语,用于表示两个对象之间的单向关系 。 例如, 学生可以引用 地址类,反之亦然。

In Java, aggregation represents HAS-A relationship, which means when a class contains reference of another class known to have aggregation.

在Java中,聚合表示HAS-A关系 ,这意味着当一个类包含另一个已知具有聚合的类的引用时。

The HAS-A relationship is based on usage, rather than inheritance. In other words, class A has-a relationship with class B, if class A has a reference to an instance of class B.

HAS-A关系基于用法而不是继承。 换句话说,如果类A引用了类B的实例,则类A与类B具有关系。

Lets understand it by an example and consider two classes Student and Address. Each student has own address that makes has-a relationship but address has student not makes any sense. We can understand it more clearly using Java code.

让我们通过一个例子来理解它,并考虑两个类Student和Address。 每个学生都有自己的联系地址,但地址与学生无关。 使用Java代码,我们可以更清楚地了解它。

Class Address{
int street_no;
String city;
String state;
int pin;
Address(int street_no, String city, String state, int pin ){
this.street_no = street_no;
this.city = city;
this.state = state;
this.pin = pin;
}
}class Student
{String name;Address ad;
}

Here in the above code, we can see Student class has-a relationship with Address class. We have draw an image too to demonstrate relation between these two classes..

在上面的代码中,我们可以看到Student类与Address类具有某种关系。 我们也绘制了一个图像来演示这两个类之间的关系。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部