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 application code example: 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)
Fig 1.1 Generalization relationship between animal class, Tiger class, and dog class
[Code performance]
- ClassAnimal {}
- ClassTiger extends animal {}
- PublicClass Test
- {
- Public void test ()
- {
- Animal A = new tiger ();
- }
- }
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]
- PublicClass person {
- /** Screw */
- Public void screw (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)
Fig 1.3Association between companies and employees
[Code performance]
- PublicClass 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]
- PublicClass computer {
- Private CPU;
- Public CPU getcpu (){
- Return CPU;
- }
- Public void setcpu (CPU ){
- This. CPU = CPU;
- }
- // Enable the computer
- Public void start (){
- // CPU operation
- CPU. Run ();
- }
- }
|