The relationship between classes plays an important role in understanding object-oriented objects. I was often asked this question during interviews. Here I will introduce it.
There is a relationship between the class and the class:
(1) Generalization)
(2) Association)
(3) Dependency)
(4) Aggregation)
UML diagram and ApplicationCodeExample:
1. Generalization)
[Generalization]
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] (Fig. 1.1)
Figure 1.1 generalized relationship between animal class and tiger class and Dog class
[Code performance]
- Class Animal {}
- ClassTigerExtendsAnimal {}
- Public ClassTest
- {
- Public VoidTest ()
- {
- Animal A =NewTiger ();
- }
- }
2. Dependency)
[Dependency]
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]
Dependencies are manifested in local variables, method parameters, and calls to 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 representation] (Figure 1.2)
Figure 1.2 dependency between the person class and the screwdriver class
[Code performance]
- Public ClassPerson {
- /** Screw */
- Public VoidScrew (screwdriver ){
- Screwdriver. Screw ();
- }
- }
3. Association)
[Association]
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]
Associations are implemented using instance variables.
[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] (Fig. 1.3)
Figure 1.3 association between companies and employees
[Code performance]
Public class employee
{
Public void startworking ()
{
}
}
Public class company
{
Private employee;
Public Employee getemployee ()
{
Return employee;
}
Public void setemployee (employee Employee)
{
This. Employee = employee;
}
// Company operation
Public void run ()
{
Employee. startworking ();
}
}
(4) Aggregation)
[Aggregation]
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. It is a strong association, emphasizing the relationship between the whole and the part.
[Specific performance]
Like the association relationship, the aggregation relationship is also implemented through instance variables. There is no way to distinguish between associations and aggregation syntactically. In terms of semantics, the difference between the two can be better distinguished.
[Differences between Association and aggregation]
(1) 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.
(2) For two objects with a clustering relationship (especially a strong clustering relationship), the overall object will restrict the lifecycle of the object that consists of it. 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 lifecycle disappears, the partial lifecycle disappears. For example, if Michael's computer is stolen, all the components of the computer do not exist, unless Michael has removed some computer components (such as hard disks and memory) beforehand.
[UML diagram] (Fig. 1.4)
Figure 1.3 computer and component Aggregation
[Code performance]
-
- Public ClassComputer {
-
- PrivateCPU;
-
- PublicCPU getcpu (){
- ReturnCPU;
-
- }
-
- Public VoidSetcpu (CPU ){
-
- This. CPU = CPU;
-
- }
-
- // Enable the computer
-
- Public VoidStart (){
-
- // CPU operation
- CPU. Run ();
-
}
from Network
Note 1:
Association ( association ) describes the semantic connections between objects of a given class . Associations provide connections between different interaction objects. The rest is related to the description of the category, rather than their instances.
from: the uniied modeling language reference manual
by James Rumbaugh, Ivar jacob, grady booch
interpreted by ADAMS Wang
therefore, if there is no object, there is no association relationship. In the Program , if the static method of the class is called but the instance of the class is not generated, the objects of these two classes are not correlated . However, classes are dependent .