UML class diagram (3)

Source: Internet
Author: User
Relationship between classes (2)

2. Dependency

Dependency relationshipA relationship is used. Changes to a specific thing may affect other things that use this thing, and dependencies must be used to indicate that one thing uses another.In most cases, dependency is reflected in the method of a class using the object of another class as the parameter.In UML, dependency is represented by a dotted line with arrows, and the dependent party points to the dependent party. For example, when a driver is driving, the car object of the car type is passed as a parameter in the drive () method of the driver class, so that the car's move () method can be called in the drive () method, the driver's drive () method depends on the vehicle's move () method. Therefore, the driver class depends on the car class, as shown in 1:

Figure 1 Dependency instance

In the system implementation phase, dependency is usually implemented in three ways. The first and most common method is shown in 1.Use the object of a class as the parameter of the method in another class., The second method isIn a class method, the object of another class is used as its local variable., The third method isCall the static method of another class in the method of one class. Figure 1 shows the corresponding Java code snippet:

public class Driver {public void drive(Car car) {car.move();}    ……}public class Car {public void move() {......}    ……}

3. Generalized relationship

A generalized relationship is an inheritance relationship used to describe the relationship between a parent class and a subclass. A parent class is also called a base class or a superclass, and a subclass is also called a derived class. In UML, generalized relationships are represented by straight lines with hollow triangles.During code implementation, we use an Object-oriented Inheritance mechanism to implement generalized relationships. For example, in Java, we use the extends keyword and the colon ":. For example, student and teacher classes are subclasses of the person class. student and teacher classes inherit the attributes and methods of the person class. The attributes of the person class include names) and age, each student and teacher also have these two attributes. In addition, the student class adds the studentno, and the teacher class adds the attribute instructor number (teacherno ), the methods of the person class include walking move () and talking say (). The student class and the teacher class inherit the two methods, and the student class also adds the Method Study (), the teacher class also adds the teach () method (). 2:

Figure 2 generalized link instance

The Java code snippets corresponding to Figure 2 are as follows:

// Parent class public class person {protected string name; protected int age; Public void move (){......} Public void say (){...... } // Subclass public class student extends person {private string studentno; Public void Study (){...... } // Subclass public class teacher extends person {private string teacherno; Public void teach (){...... }}

4. Interface and implementation relationship

Interfaces are introduced in many object-oriented languages, such as Java and C #. In interfaces, there are usually no attributes, and all operations are abstract, with only the declaration of operations, no operation implementation. In UML, the interface is represented in a way similar to the class notation, as shown in 3:

Figure 3 UML diagram of the interface

Interfaces can also have inheritance and dependency relationships similar to the relationships between classes, but there is also a realization relationship between interfaces and classes. In this relationship, the class implements the interface, and the operations in the class implement the Operations declared in the interface.In UML, the implementation relationship between classes and interfaces is expressed by dotted lines with hollow triangles.For example, a vehicle interface vehicle is defined, which contains an abstract move () operation. This move () operation is implemented in class ship and class car, however, the specific implementation details will be different, as shown in Figure 4:

Figure 4 link instance implementation

During programming, different object-oriented languages also provide different syntaxes, such as using the implements keyword in Java, in C ++/C #, use the colon. Figure 4 shows the corresponding Java code snippet:

public interface Vehicle {public void move();}public class Ship implements Vehicle {public void move() {    ……    }}public class Car implements Vehicle {public void move() {    ……    }}

[Author: Liu Wei http://blog.csdn.net/lovelion]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.