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类具有某种关系。 我们也绘制了一个图像来演示这两个类之间的关系。
The Student
class has an instance variable of type Address
. As we have a variable of type Address
in the Student
class, it can use Address reference which is ad
in this case, to invoke methods of the Address
class.
Student
类具有类型为Address
的实例变量。 由于我们在Student
类中具有Address
类型的变量,因此它可以使用Address引用(在这种情况下为ad
来调用Address
类的方法。
The main advantage of using aggregation is to maintain code re-usability. If an entity has a relation with some other entity than it can reuse code just by referring that.
使用聚合的主要优点是保持代码的可重用性 。 如果一个实体与某个其他实体有关系,则仅通过引用即可重用代码。
Now lets understand it by using an example, here we created two classes Author and Book and Book class has a relation with Author class by having its reference. Now we are able to get all the properties of both class.
现在让我们通过一个例子来理解它,这里我们创建了两个类Author和Book,并且Book类通过引用与Author类建立了联系。 现在,我们可以获取两个类的所有属性。
class Author
{
String authorName;
int age;
String place;
// Author class constructor
Author(String name, int age, String place)
{
this.authorName = name;
this.age = age;
this.place = place;
}
}
class Book
{
String name;
int price;
// author details
Author auther;
Book(String n, int p, Author auther)
{
this.name = n;
this.price = p;
this.auther = auther;
}
public static void main(String[] args) {
Author auther = new Author("John", 42, "USA");
Book b = new Book("Java for Begginer", 800, auther);
System.out.println("Book Name: "+b.name);
System.out.println("Book Price: "+b.price);
System.out.println("------------Auther Details----------");
System.out.println("Auther Name: "+b.auther.authorName);
System.out.println("Auther Age: "+b.auther.age);
System.out.println("Auther place: "+b.auther.place);
}
}
Book Name: Java for Begginer Book Price: 800 ------------Author Details---------- Auther Name: John Auther Age: 42 Auther place: USA
书籍名称:Java for Begginer书籍价格:800美元------------作者详细信息----------奥瑟(Auther)姓名:约翰·奥瑟(John Auther)年龄:42奥瑟(Auther)所在地:美国
Lets take one more example to understand aggregation. Suppose we have one more class Publisher then the Book class can reuse Publisher class details by just using its reference as Author class. Lets understand it by Java code.
让我们再举一个例子来了解聚合。 假设我们再有一个Publisher类,则Book类可以通过仅将其引用用作Author类来重用Publisher类的详细信息。 让我们通过Java代码来理解它。
class Publisher{
String name;
String publisherID;
String city;
Publisher(String name, String publisherID, String city) {
this.name = name;
this.publisherID = publisherID;
this.city = city;
}
}
class Author
{
String authorName;
int age;
String place;
// Author class constructor
Author(String name, int age, String place)
{
this.authorName = name;
this.age = age;
this.place = place;
}
}
class Book
{
String name;
int price;
// author details
Author auther;
Publisher publisher;
Book(String n, int p, Author auther, Publisher publisher )
{
this.name = n;
this.price = p;
this.auther = auther;
this.publisher = publisher;
}
public static void main(String[] args) {
Author auther = new Author("John", 42, "USA");
Publisher publisher = new Publisher("Sun Publication","JDSR-III4", "LA");
Book b = new Book("Java for Begginer", 800, auther, publisher);
System.out.println("Book Name: "+b.name);
System.out.println("Book Price: "+b.price);
System.out.println("------------Author Details----------");
System.out.println("Auther Name: "+b.auther.authorName);
System.out.println("Auther Age: "+b.auther.age);
System.out.println("Auther place: "+b.auther.place);
System.out.println("------------Publisher Details-------");
System.out.println("Publisher Name: "+b.publisher.name);
System.out.println("Publisher ID: "+b.publisher.publisherID);
System.out.println("Publisher City: "+b.publisher.city);
}
}
Book Name: Java for Begginer Book Price: 800 ------------Author Details---------- Auther Name: John Auther Age: 42 Auther place: USA ------------Publisher Details------- Publisher Name: Sun Publication Publisher ID: JDSR-III4 Publisher City: LA
书籍名称:Java for Begginer书籍价格:800美元------------作者详细信息----------奥瑟姓名:约翰·奥瑟年龄:42岁奥瑟所在的地方:美国--------发布者详细信息-------出版者名称:Sun出版者出版者ID:JDSR-III4出版者城市:LA
Composition is a more restricted form of Aggregation. Composition can be described as when one class which includes another class, is dependent on it in such a way that it cannot functionally exist without the class which is included. For example a class Car
cannot exist without Engine
, as it won't be functional anymore.
组合是更受限制的聚合形式。 可以将组成描述为当一个包含另一个类的类以某种方式依赖于它时,如果没有包含的类,它就无法在功能上存在。 例如,如果没有Engine
,则Car
类将不存在,因为它将不再起作用。
Hence the word Composition which means the items something is made of and if we change the composition of things they change, similarly in Java classes, one class including another class is called a composition if the class included provides core functional meaning to the outer class.
因此,“ 组成 ”一词的意思是构成某物品的物品,如果我们更改它们改变的事物的组成,则在Java类中,如果包含的类为外部类提供了核心的功能含义,则包含另一类的一个类称为“组合”。
class Car
{
private Engine engine;
Car(Engine en)
{
engine = en;
}
}
Here by examining code, we can understand that if Car class does not have relationship with Engine class then Car does not have existence.
在这里通过检查代码,我们可以理解,如果Car类与Engine类没有关系,那么Car就不存在。
Composition is a design technique, not a feature of Java but we can achieve it using Java code.
合成是一种设计技术 ,不是Java的功能,但是我们可以使用Java代码来实现。
When you want to use some property or behaviour of any class without the requirement of modifying it or adding more functionality to it, in such case Aggregation is a better option because in case of Aggregation we are just using any external class inside our class as a variable.
如果您想使用任何类的某些属性或行为而无需对其进行修改或为其添加更多功能,则在这种情况下, 聚合是一个更好的选择,因为在聚合的情况下,我们只是将类内部的任何外部类用作变量。
Whereas when you want to use and modify some property or behaviour of any external class or may be want to add more function on top of it, its best to use Inheritance.
而当您要使用和修改任何外部类的某些属性或行为,或者可能想在其之上添加更多功能时,最好使用Inheritance 。
To understand more on inheritance, you can visit our detailed tutorial here. Click Here to see Inheritance in Java
要了解有关继承的更多信息,您可以在此处访问我们的详细教程。 单击此处查看Java继承
翻译自: https://www.studytonight.com/java/aggregation.php
java合成关系和聚合关系