There is a relationship between the class and the class:
- Generalization)
- Dependency)
- Association)
- Aggregation)
1. Generalization)
Definition:Represents the inheritance relationship between classes, the inheritance relationship between interfaces, or the implementation relationship between classes on interfaces. The general relationship is directed from the subclass to the parent class, which is opposite to the inherited or implemented method.
Specific performance:Parent classParent class instance = newSubclass()
UML diagram,Generalization relationship between Animal class, Tiger class, and Dog class
:
Code performance:
The code is as follows: |
Copy code |
Class Animal {} Class Tiger extends Animal {} Public class Test { Public void test (){ Animal a = new Tiger (); } } |
2. Dependency)
Definition:For two relatively independent objects, when one object is responsible for constructing an instance of another object or dependent on the service of another object, the two objects are mainly dependent on each other.
Specific performance:Dependency is manifested in
Local variable,
Method parameters, And
Call static methods.
Real-world example:For example, if you want to screw the screw, do you want to use (that is, rely on) the screw driver to help you complete the screw (screw) work?
UML diagram:Dependency between the Person class and the Screwdriver class
Code performance:
The code is as follows: |
Copy code |
Public class Person { /* Screw */ Public void screw (Screwdriver screwdriver ){ Screwdriver. screw (); } } |
3. Association)
Definition:For two relatively independent objects, when the instance of one object has a fixed ing relationship with some specific instances of the other object, the two objects are associated.
Specific performance:Link is used
Instance variablesTo achieve
Real-world example:For example, for customers and orders, each order corresponds to a specific customer, and each customer corresponds to a specific order. For example, for companies and employees, each company corresponds to a specific employee, each employee corresponds to a specific company.
UML diagram, The relationship between the company and its employees:
Code performance:
The code is as follows: |
Copy code |
Public class Company { Private Employee employee; Public Employee getEmployee (){ Return employee } Public void setEmployee (Employee employee ){ This. employee = employee; } // Company operation Public vodi run (){ Employee. startWorking (); } } |
4. Aggregation)
Definition:When object A is added to object B and becomes an integral part of object B, object B and object A are clustered. Aggregation is a type of association, which is strongly correlated.
OverallAnd
Part.
Specific performance:Like an association, an aggregation link also uses
Instance variablesTo achieve this relationship. There is no way to distinguish between the link and the aggregation link.
SemanticsUpstream
Better differentiationThe difference between the two.
Differences between association and aggregation:
- The two objects involved in the Association are at the same level. For example, people and bicycles are associated rather than aggregated, because people are not composed of bicycles. The two objects involved in an aggregation relationship are at an unequal level. One represents the whole and the other represents the part. For example, the computer and its display, keyboard, Motherboard and memory are clustered, because the motherboard is part of the computer.
- For two objects with a clustering relationship (especially a strong clustering relationship), the overall object will restrict the lifecycle of the object it is composed. Objects of a partial classification cannot exist independently. The lifecycle of an object depends on the lifecycle of the object of the entire class. When the entire object disappears, the partial lifecycle disappears. For example, if Michael's computer is stolen, all the components of the computer will no longer exist, unless Michael has removed some computer components (such as hard disks and memory.
UML diagram:
Code performance:
The code is as follows: |
Copy code |
Public class Computer {
Private CPU;
Public CPU getCPU (){
Return cpu;
}
Public void setCPU (CPU cpu ){
This. cpu = cpu;
}
// Enable the computer
Public void start (){
// Cpu operation
Cpu. run ()
}
}
|