The UML diagram and code expression of the relationship between classes and classes in java. If you need it, refer to it. 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 class instance = new subclass ()
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 cpu; public CPU getCPU(){ return cpu; } public void setCPU(CPU cpu){ this.cpu = cpu; } // Enable the computer public void start(){ // Cpu operation cpu.run() } } |