Relationships between classes and interfaces: inheritance
Relationships between classes and classes: Inheritance relationships include relationships
Relationships between classes and objects: instances
Relationships between classes and classes in a UML class diagram:
Generalization relationship (generalization)
Association Relationship (Association)
Aggregation Relationship (aggregation)
Synthesis Relationship (Compostion)
Dependency relationship (dependency)
1. Generalization (generalization)
[generalization] represents an inheritance relationship between a class and a class, an inheritance relationship between an interface and an interface, or a class-to-interface implementation relationship. A generalized relationship is from a subclass to a parent class, as opposed to an inherited or implemented method.
[UML Diagram] (Fig. 1.1)
Figure 1.1 generalization relationship between animal class and Tiger class and Dog class
[Code Performance]
- class animal{}
- class Tiger extends animal{}
- Public class Test {
- Public void test () {
- Animal a=new Tiger ();
- }
- }
2. Association (Association)
[Association] for two relatively independent objects, when an instance of one object has a fixed correspondence with some particular instance of another object, the two objects are associated.
1. An association is a junction between a class and a class that enables a class to know the properties and methods of another class.
2, the association can be bidirectional, or can be one-way. Two-way associations can have two arrows or no arrows, and one-way associations have an arrow.
3. In Java or C + +, association relationships are implemented by using member variables.
[Specific performance]
Association relationships are implemented using instance variables //Except for these three kinds of outside me (not me! Want to also include some instance variables to implement, just as relational and aggregation relationships are syntactically indistinguishable. Dependencies and associations are also difficult to distinguish in some ways from grammar.
[Example of Reality]
For example, the customer and the order, each order corresponds to a specific customer, each customer corresponds to some specific orders, such as companies and employees, each company corresponds to some specific employees, each employee corresponds to a specific company
[UML Diagram] (Fig. 1.2)
Figure 1.2 the relationship between company and employee
[Code Performance]
- Public class Company {
- private employee employee;
- Public Employee getemployee () {
- return employee;
- }
- Public void setemployee (Employee employee) {
- This . Employee=employee;
- }
- //company Operation
- Public void run () {
- Employee.startworking ();
- }
- }
- Public class Person {
- private screwservice screwservice;
- Public screwservice getscrewservice () {
- return screwservice;
- }
- Public void setscrewservice (screwservice screwservice) {
- This . screwservice = Screwservice;
- }
- Public void screw () {
- Screwservice.screw ();
- }
- }
Class Apprentice {};
Class Tang Monk {
Protected
list< Apprentice > Tdlist;
};
3. Aggregation (Aggregation)
[Aggregation] when object A is added to object B and becomes part of Object B, an aggregation relationship is between object B and object A. Aggregation is one kind of correlation relation, it is strong association relation, emphasizing the relation between whole and part .
1, the aggregation relation is one kind of correlation relation, is the strong correlation relation.
2, aggregation is the relationship between the whole and part, for example, the car by the engine, tires and other components.
3, the aggregation relationship is also achieved through member variables. ButThe two classes involved in an association relationship are at the same level, while in the aggregation relationship, two classes are at different levels, one represents the whole, and one represents the part。
4. Correlation and aggregation are indistinguishable only from Java or C + + syntax, and must be examined in relation to the logical relationships among the classes involved.
[Specific performance]
As with association relationships, the aggregation relationship is alsoinstance VariableTo achieve such a relationship. Associative relationships and aggregation relationships are syntactically indistinguishable, fromSemanticsThe ability toa better distinctionThe difference between the two.
[Differences between associations and aggregations]
(1) The two objects involved in the association relationship are at the same level. For example, people and bicycles are an association, not an aggregation, because people are not made up of bicycles.
The two objects involved in the aggregation relationship are at an unequal level, one representing the whole, and the other representing the part. For example, the computer and its monitor, keyboard, motherboard and memory are aggregation relationships, because the motherboard is a part of the computer.
(2) for two objects with aggregation relationships, especially strong aggregation relationships, the overall object restricts the life cycle of its constituent objects. The object of a partial class cannot exist alone, its life cycle depends on the life cycle of the object of the whole class, and when the whole disappears, the part disappears. For example, Zhang San's computer is stolen, then all the components of the computer are not there, unless Zhang San in advance to remove some of the computer components (such as hard disk and memory).
[UML diagram] (Figure 1.3)
Figure 1.3 The aggregation relationship of computers and components
[code performance]
- Public class computer{
- private CPU CPU;
- Public CPU getcpu () {
- return CPU;
- }
- Public void SetCPU (CPU CPU) {
- This . cpu=cpu;
- }
- //Turn on the computer
- Public void start () {
- Cpu.run (); //cpu Operation
- }
- }
Class Engine {};
class Tires {};
Class Car {
Protected
Engine engines;
Tyre tyre[4];
};
4. Synthesis (composition)
Synthesis
1, the synthesis relation is one kind of correlation relation, is also stronger than the aggregation relation.
2, it requires the ordinary aggregation relationship represents the whole object is responsible for representing the part of the object's life cycle.
Class Limb {};
Class Person {
Protected
Limb limb[4];
};
5. Dependency (Dependency)
[dependent] for two relatively independent objects, when an object is responsible for constructing an instance of another object, or a service that relies on another object, the two objects are primarily dependencies.
1. Dependency is also the connection between class and class
2, dependence is always one-way.
3. Dependencies are represented in the Java or C + + language as local variables, parameters of methods, or calls to static methods.
[Specific performance]
Dependencies are represented in local variables , parameters of methods , and calls to static methods
[Example of Reality]
For example, if you are going to screw the screws, do you want to use (i.e. rely on) a screwdriver (screwdriver) to help you complete the screw (screw) work
[UML Performance] (Fig. 1.4)
Figure 1.4 dependency between the person class and the screwdriver class
[Code Performance]
- Public class person{
- Public void screw (screwdriver screwdriver) {
- Screwdriver.screw ();
- }
- }
public class Person {
public void Buy (car car) {
...
}
}
Summarize:
1. Different relationships using different UML legends
2, for the aggregation relation, the synthesis relation, because all is one kind of correlation relation, therefore in the uncertain situation, may use the association relation to describe them.
The relationship between Java classes and classes and UML diagrams