មេរៀនទី១៤: ការប្រមូលផ្តុំ Aggregation

ប្រសិន class មានទំនាក់ទំនង entity ដូចជាការប្រមូលផ្តុំទំនាក់ទំនង។ Employee object ផ្ទុកពត៌មានជាច្រើនដូចជា id, name, emailId ។ល។ វាផ្ទុកឈ្មោះ object មួយឬច្រើន។ ដែលវាផ្ទុកព័ត៌មានផ្ទុកដូចជា city, state, country, zipcode បង្ហាញដូចខាងក្រោម
class Employee{
int id;
String name;
Address address;//Address is a class

}
នៅក្នុងករណីនេះ, Employee មាន entity មួយដូច្នេះ ទំនាក់ទំងគឺជា Employee។
ហេតុអ្វីប្រើ Aggregation?
ឧទាហរណ៍នៃ Aggregation
Javaនៅក្នុងឧទាហរណ៍ យើងបានបង្កើត Operation class ក្នុង Circle class.
class Operation{
int square(int n){
return n*n;
}
}
class Circle{
Operation op;//aggregation
double pi=3.14;
1.     double area(int radius){  
op=new Operation();
int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).
return pi*rsquare;
}
public static void main(String args[]){
Circle c=new Circle();
double result=c.area(5);
System.out.println(result);
}
}  Output:78.5
ស្វែងយល់ពីន័យនៃការប្រើ Aggregation
Address.java
public class Address {
String city,state,country;
public Address(String city, String state, String country) {
this.city = city;
this.state = state;
this.country = country;
}
}
Emp.java
public class Emp {
int id;
String name;
Address address;
public Emp(int id, String name,Address address) {
this.id = id;
this.name = name;
this.address=address;
}
void display(){
System.out.println(id+” “+name);
System.out.println(address.city+” “+address.state+” “+address.country);
}
public static void main(String[] args) {
Address address1=new Address(“gzb”,”UP”,”Cambodia”);
Address address2=new Address(“gno”,”UP”,” Cambodia “);
Emp e=new Emp(111,”varun”,address1);
Emp e2=new Emp(112,”arun”,address2);
e.display();
e2.display();
}
}
Output:111 varun
gzb UP Cambodia
112 arun
gno UP Cambodia
Download ឧទាហរណ៍នេះ