Java 简明教程
Java - Aggregation
Java Aggregation
聚合是两个 classes 之间的关系,其中一个类包含另一个类的实例。例如,当对象 A 包含对另一个对象 B 的引用或我们可以说对象 A 与对象 B 具有 HAS-A 关系时,在 Java 编程中称为聚合。
An aggregation is a relationship between two classes where one class contains an instance of another class. For example, when an object A contains a reference to another object B or we can say Object A has a HAS-A relationship with Object B, then it is termed as Aggregation in Java Programming.
Use of Java Aggregation
Java 中的聚合有助于重用代码。对象 B 可以具有实用程序方法,并且可以由多个对象利用。无论哪个类拥有对象 B,它都可以利用其方法。
Aggregation in Java helps in reusing the code. Object B can have utility methods and which can be utilized by multiple objects. Whichever class has object B then it can utilize its methods.
Java Aggregation Examples
Example 1
在此示例中,我们创建了若干类,如车辆、速度。定义了一个扩展车辆类的面包车类,并具有一个速度类对象。面包车类 inherits 从车辆类获取属性,速度作为其属性,我们从调用方对象传递。在输出中,我们打印面包车对象的详细信息。
In this example, we’re creating few classes like Vehicle, Speed. A Van class is defined which extends Vehicle class and has a Speed class object. Van class inherits properties from Vehicle class and Speed being its property, we’re passing it from caller object. In output, we’re printing the details of Van object.
package com.tutorialspoint;
class Vehicle{
private String vin;
public String getVin() {
return vin;
}
public void setVin(String vin) {
this.vin = vin;
}
}
class Speed{
private double max;
public double getMax() {
return max;
}
public void setMax(double max) {
this.max = max;
}
}
class Van extends Vehicle {
private Speed speed;
public Speed getSpeed() {
return speed;
}
public void setSpeed(Speed speed) {
this.speed = speed;
}
public void print() {
System.out.println("Vin: " +this.getVin() + ", Max Speed: " + speed.getMax() );
}
}
public class Tester {
public static void main(String[] args) {
Speed speed = new Speed();
speed.setMax(120);
Van van = new Van();
van.setVin("abcd1233");
van.setSpeed(speed);
van.print();
}
}
Vin: abcd1233, Max Speed: 120.0
Example 2
在此示例中,我们正在创建一些类,例如 Student、Address。学生可以拥有地址。因此,我们在 Student 类中将地址定义为实例变量。在输出中,我们正在打印学生详细信息。
In this example, we’re creating few classes like Student, Address. A student can has a address. So we’ve defined an address as an instance variable in Student class. In output, we’re printing the student details.
package com.tutorialspoint;
class Address {
int strNum;
String city;
String state;
String country;
Address(int street, String c, String st, String country) {
this.strNum = street;
this.city = c;
this.state = st;
this.country = country;
}
}
class Student {
int rno;
String stName;
Address stAddr;
Student(int roll, String name, Address address){
this.rno = roll;
this.stName = name;
this.stAddr = address;
}
}
public class Tester {
public static void main(String[] args) {
Address ad= new Address(10, "Bareilly", "UP", "India");
Student st= new Student(1, "Aashi", ad);
System.out.println("Roll no: "+ st.rno);
System.out.println("Name: "+ st.stName);
System.out.println("Street: "+ st.stAddr.strNum);
System.out.println("City: "+ st.stAddr.city);
System.out.println("State: "+ st.stAddr.state);
System.out.println("Country: "+ st.stAddr.country);
}
}
Roll no: 1
Name: Aashi
Street: 10
City: Bareilly
State: UP
Country: India
在独特的意义上,这是一种联合。聚合是一种单向定向关系,它准确地表示类之间的 HAS-A 关系。此外,当两个类聚合时,终止其中一个类对另一个类没有影响。与组成相比,它经常被指定为弱关系。相比之下,父对象拥有子实体,这意味着无法直接访问子实体,并且如果没有父对象,子实体无法存在。相反,在关联中,父实体和子实体都可以以自己的权利存在。
In a unique sense, this is a type of association. Aggregation is a one way directed relationship that precisely expresses HAS-A relationship between classes. Additionally, when two classes are aggregated, terminating one of them has no effect on the other. When compared to composition, it is frequently designated as a weak relationship. In comparison, the parent owns the child entity, which means the child entity cannot be accessed directly and cannot exist without the parent object. Contrarily, in an association, the parent and child entities may both exist in their own right.
HAS-A Relationship
这些关系主要基于使用情况。这确定了某个类是否 HAS-A 某些事情。这种关系有助于减少代码冗余和错误。
These relationships are mainly based on the usage. This determines whether a certain class HAS-A certain thing. This relationship helps to reduce duplication of code as well as bugs.
Example
public class Vehicle{}
public class Speed{}
public class Van extends Vehicle {
private Speed sp;
}
这表明类 Van HAS-A Speed。通过为 Speed 单独创建一个类,我们不必将属于 speed 的所有代码都放入 Van 类中,这使得可以在多个应用程序中重用 Speed 类。
This shows that class Van HAS-A Speed. By having a separate class for Speed, we do not have to put the entire code that belongs to speed inside the Van class, which makes it possible to reuse the Speed class in multiple applications.
在面向对象特性中,用户不需要操心由哪个对象来完成实际工作。要实现此点,Van 类对 Van 类用户隐藏实现细节。因此,基本上发生的事情是用户会要求 Van 类执行某个动作,而 Van 类会自己完成工作或要求另一个类执行此动作。
In Object-Oriented feature, the users do not need to bother about which object is doing the real work. To achieve this, the Van class hides the implementation details from the users of the Van class. So, basically what happens is the users would ask the Van class to do a certain action and the Van class will either do the work by itself or ask another class to perform the action.