Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the relationship between classes in UML class diagram

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relationship between classes and classes in the UML class diagram, which is very detailed and has a certain reference value. Interested friends must read it!

The relationship between classes in UML Class Diagram

There are the following relationships between classes:

(1) Generalization

(2) Association (Association)

(3) dependence (Dependency)

(4) aggregation (Aggregation)

1. Generalization (Generalization)

The generalization in the UML class diagram represents the inheritance relationship between classes, the inheritance relationship between interfaces, or the implementation relationship between classes and interfaces. The generalized relationship is directed from the subclass to the parent class, as opposed to the inheritance or implementation method.

[specific performance]

Parent class instance = new subclass ()

[UML figure] (figure 1.1)

The generalization relationship between 1.1Animal class, Tiger class and Dog class.

[code performance]

[code performance]

Java code

ClassAnimal {classTigerextendsAnimal {} publicclassTest {publicvoidtest () {Animala=newTiger ();}}

two。 Dependency (Dependency)

UML class diagrams always rely on two relatively independent objects, when one object is responsible for constructing an instance of another object, or relying on the services of another object, the relationship between the two objects is mainly a dependency.

[specific performance]

Dependencies are shown in local variables, parameters of methods, and calls to static methods

[practical example]

For example, if you are going to screw, do you need to use (that is, rely on) a screwdriver (Screwdriver) to help you with the work of screw?

[UML performance] (figure 1.2)

Figure the dependency between 1.2Person class and Screwdriver class

[code performance]

Java code

PublicclassPerson {/ * * screw * / publicvoidscrew (Screwdriverscrewdriver) {screwdriver.screw ();}}

3. Association (Association)

Associations in the UML class diagram. For two relatively independent objects, there is an association between the two objects when there is a fixed corresponding relationship between the instance of one object and some specific instances of another object.

[specific performance]

The association relationship is implemented using instance variables.

[practical example]

For example, customers and orders, each order corresponds to a specific customer, each customer corresponds to a specific order; another example is a company and employees, each company corresponds to some specific employees, each employee corresponds to a specific company

[UML figure] (figure 1.3)

Figure 1.3 relationship between the company and its employees

[code performance]

Java code

PublicclassCompany {privateEmployeeemployee; publicEmployeegetEmployee () {returnemployee;} publicvoidsetEmployee (Employeeemployee) {this.employee=employee;} / / Company operates publicvoidrun () {employee.startWorking ();}}

(4) aggregation (Aggregation)

The aggregation relationship in UML class diagram means that when object An is added to object B and becomes a part of object B, there is an aggregation relationship between object B and object A. Aggregation is a kind of relevance relationship, which is a strong relationship, which emphasizes the relationship between the whole and the part.

[specific performance]

Like association relationships, aggregation relationships are implemented through instance variables. There is no way to distinguish between relevance relation and aggregation relation in syntax, but the difference between them can be better distinguished semantically.

[the difference between association and aggregation]

The main results are as follows: (1) the two objects involved in the relationship are at the same level. For example, people and bicycles are related, not aggregative, because people are not made up of bicycles.

The two objects involved in the aggregation relationship are at the level of inequality, one represents the whole and the other represents the part. For example, there is an aggregation relationship between the computer and its monitor, keyboard, motherboard and memory, because the motherboard is part of the computer.

(2) for two objects with aggregation relations (especially strong aggregation relations), the whole object will restrict the life cycle of its constituent objects. The object of part of the class can not exist alone, its life cycle depends on the life cycle of the object of the whole class, when the whole disappears, the part also disappears. For example, if Zhang San's computer is stolen, then all the components of the computer no longer exist, unless Zhang San removes some computer components (such as hard disk and memory) beforehand.

[UML figure] (figure 1.4)

Figure 1.3 aggregation of computers and components

[code performance]

Java code

PublicclassComputer {privateCPUcpu; publicCPUgetCPU () {returncpu;} publicvoidsetCPU (CPUcpu) {this.cpu=cpu;} / turn on computer publicvoidstart () {/ / cpu operation cpu.run ();} above is all the content of the article "what is the relationship between classes in UML class diagram". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report