Java中的关联、组合和聚合

关联是通过对象建立的两个独立类之间的关系。关联可以是一对一、一对多、多对一、多对多。在面向对象编程中,一个对象与另一个对象通信,以使用该对象提供的功能和服务。 作文 聚集 是两种形式的联系。

null

Association,Aggregation and Composition

例子:

JAVA

// Java Program to illustrate the
// Concept of Association
// Importing required classes
import java.io.*;
// Class 1
// Bank class
class Bank {
// Attribures of bank
private String name;
// Constructor of this class
Bank(String name)
{
// this keyword refers to current instance itself
this .name = name;
}
// Method of Bank class
public String getBankName()
{
// Returning name of bank
return this .name;
}
}
// Class 2
// Employee class
class Employee {
// Attribures of employee
private String name;
// Employee name
Employee(String name)
{
// This keyword refwrs to current insytance itself
this .name = name;
}
// Method of Employee class
public String getEmployeeName()
{
// returning the name of employee
return this .name;
}
}
// Class 3
// Association between both the
// classes in main method
class GFG {
// Main driver mmethod
public static void main(String[] args)
{
// Creating objects of bank and Employee class
Bank bank = new Bank( "ICICI" );
Employee emp = new Employee( "Ridhi" );
// Print and display name and
// corresponding bank of employee
System.out.println(emp.getEmployeeName()
+ " is employee of "
+ bank.getBankName());
}
}


输出

Ridhi is employee of ICICI

输出说明: 在上面的示例中,两个独立的类Bank和Employee通过其对象关联。银行可以有很多员工,所以是一对多的关系。

Association in Java

聚集

这是一种特殊的结社形式,其中:

  • 它代表了Has-A的关系。
  • 这是一个 单向联想 i、 e.单向关系。例如,一个系可以有学生,但反之亦然是不可能的,因此是单向的。
  • 总之, 两个条目都可以单独保存 这意味着终止一个实体不会影响另一个实体。
图片[3]-Java中的关联、组合和聚合-yiteyi-C++库

聚集

实例

JAVA

// Java program to illustrate Concept of Aggregation
// Importing required classes
import java.io.*;
import java.util.*;
// Class 1
// Student class
class Student {
// Attributes of student
String name;
int id;
String dept;
// Constructor of student class
Student(String name, int id, String dept)
{
// This keyword refers to current instance itself
this .name = name;
this .id = id;
this .dept = dept;
}
}
// Class 2
// Department class contains list of student objects
// It is associated with student class through its Objects
class Department {
// Attributes of Department class
String name;
private List<Student> students;
Department(String name, List<Student> students)
{
// this keyword refers to current instance itself
this .name = name;
this .students = students;
}
// Method of Department class
public List<Student> getStudents()
{
// Returning list of user defined type
// Student type
return students;
}
}
// Class 3
//  Institute class contains list of Department
// Objects. It is associated with Department
// class through its Objects
class Institute {
// Attributes of Institute
String instituteName;
private List<Department> departments;
// Constructor of institute class
Institute(String instituteName,List<Department> departments)
{
// This keyword refers to current instance itself
this .instituteName = instituteName;
this .departments = departments;
}
// Method of Institute class
// Counting total students of all departments
// in a given institute
public int getTotalStudentsInInstitute()
{
int noOfStudents = 0 ;
List<Student> students;
for (Department dept : departments) {
students = dept.getStudents();
for (Student s : students) {
noOfStudents++;
}
}
return noOfStudents;
}
}
// Class 4
// main class
class GFG {
// main driver method
public static void main(String[] args)
{
// Creating object of Student class inside main()
Student s1 = new Student( "Mia" , 1 , "CSE" );
Student s2 = new Student( "Priya" , 2 , "CSE" );
Student s3 = new Student( "John" , 1 , "EE" );
Student s4 = new Student( "Rahul" , 2 , "EE" );
// Creating a List of CSE Students
List<Student> cse_students = new ArrayList<Student>();
// Adding CSE students
cse_students.add(s1);
cse_students.add(s2);
// Creating a List of EE Students
List<Student> ee_students
= new ArrayList<Student>();
// Adding EE students
ee_students.add(s3);
ee_students.add(s4);
// Creating objects of EE and CSE class inside
// main()
Department CSE = new Department( "CSE" , cse_students);
Department EE = new Department( "EE" , ee_students);
List<Department> departments = new ArrayList<Department>();
departments.add(CSE);
departments.add(EE);
// Lastly creating an instance of Institute
Institute institute = new Institute( "BITS" , departments);
// Display message for better readability
System.out.print( "Total students in institute: " );
// Calling method to get total number of students
// in institute and printing on console
System.out.print(institute.getTotalStudentsInInstitute());
}
}


输出

Total students in institute: 4

输出说明: 在这个例子中,有一个研究所没有像CSE,EE这样的部门。每个系都有学生。因此,我们制作了一个Institute类,该类引用了Department类的Object或Object数量(即Object列表)。这意味着Institute类通过其对象与Department类相关联。Department类还引用了学生类的一个或多个对象(即对象列表),这意味着它通过其对象与学生类相关联。

它代表着一种 有一个 关系在上面的例子中:学生 有一个 名称大学生 有一个 学生证 有一个 部门 有一个 学生如以下媒体所示。

Aggregation_1

我们什么时候使用聚合?? 代码重用最好通过聚合实现。

概念3: 作文

图片[5]-Java中的关联、组合和聚合-yiteyi-C++库

作文

组合是一种受限的聚合形式,其中两个实体彼此高度依赖。

  • 它代表 部分 关系
  • 在组合中,两个实体相互依赖。
  • 当两个实体之间存在合成时,合成的对象 不可能存在 没有其他实体。

实例 图书馆

JAVA

// Java program to illustrate
// the concept of Composition
// Importing required classes
import java.io.*;
import java.util.*;
// Class 1
//  Book
class Book {
// Attributes of book
public String title;
public String author;
// Constructor of Book class
Book(String title, String author)
{
// This keyword refers to current instance itself
this .title = title;
this .author = author;
}
}
// Class 2
class Library {
// Reference to refer to list of books
private final List<Book> books;
// Library class contains list of books
Library(List<Book> books)
{
// Referring to same book as
// this keyword refers to same instance itself
this .books = books;
}
// Method
// To get total number of books in library
public List<Book> getTotalBooksInLibrary()
{
return books;
}
}
// Class 3
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating objects of Book class inside main()
// method Custom inputs
Book b1
= new Book( "EffectiveJ Java" , "Joshua Bloch" );
Book b2
= new Book( "Thinking in Java" , "Bruce Eckel" );
Book b3 = new Book( "Java: The Complete Reference" ,
"Herbert Schildt" );
// Creating the list which contains number of books
List<Book> books = new ArrayList<Book>();
// Adding books
// using add() method
books.add(b1);
books.add(b2);
books.add(b3);
Library library = new Library(books);
// Calling method to get total books in library
// and storing it in list of user0defined type -
// Books
List<Book> bks = library.getTotalBooksInLibrary();
// Iterating over books using for each loop
for (Book bk : bks) {
// Printing the title and author name of book on
// console
System.out.println( "Title : " + bk.title
+ " and "
+ " Author : " + bk.author);
}
}
}


输出

Title : EffectiveJ Java and  Author : Joshua BlochTitle : Thinking in Java and  Author : Bruce EckelTitle : Java: The Complete Reference and  Author : Herbert Schildt

输出说明: 在上面的例子中,一个库可以有 关于相同或不同的主题。所以,如果图书馆被毁,那么该图书馆内的所有书籍都将被毁。i、 电子书离不开图书馆。这就是为什么它是构图。书是 部分 图书馆

聚合与合成

1.依赖性: 聚合意味着一种关系,其中 可以独立存在 父母的。例如,银行和员工,删除银行和员工仍然存在。而构图意味着孩子 不能独立存在 父母的。例:人与心,心不是独立于人而存在的

2.关系类型: 聚合关系是 “有一个” 构图是 “部分” 关系

3.协会类型: 作文是一门艺术 坚强的 关联,而聚合是一种 虚弱的 协会

例子:

JAVA

// Java Program to Illustrate Difference between
// Aggregation and Composition
// Importing I/O classes
import java.io.*;
// Class 1
// Engine class which will
// be used by car. so 'Car'
// class will have a field
// of Engine type.
class Engine {
// Method to starting an engine
public void work()
{
// Print statement whenever this method is called
System.out.println(
"Engine of car has been started " );
}
}
// Class 2
// Engine class
final class Car {
// For a car to move,
// it needs to have an engine.
// Composition
private final Engine engine;
// Note: Uncommented part refers to Aggregation
// private Engine engine;
// Constructor of this class
Car(Engine engine)
{
// This keywords refers to same instance
this .engine = engine;
}
// Method
// Car start moving by starting engine
public void move()
{
// if(engine != null)
{
// Calling method for working of engine
engine.work();
// Print statement
System.out.println( "Car is moving " );
}
}
}
// Class 3
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Making an engine by creating
// an instance of Engine class.
Engine engine = new Engine();
// Making a car with engine so we are
// passing a engine instance as an argument
// while creating instance of Car
Car car = new Car(engine);
// Making car to move by calling
// move() method inside main()
car.move();
}
}


输出

Engine of car has been started Car is moving 

在聚合的情况下,汽车还通过发动机执行其功能。但发动机并不总是汽车的内部部件。发动机可以更换,甚至可以从车上拆下。这就是为什么我们将“引擎类型”字段设为非最终字段。

本文由 尼茨海伦德拉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享