That Understanding of UML class diagram (rpm)

First, we define a very simple Person class, as follows:

public class Person {
   private String name;
   private int age =1;

   public String getName() {
      return name;
   }
   public int getAge() {
      return age;
   }
   public void setName(String name) {
      this.name=name;
   }
   public void setAge(int age) {
      this.age=age;
   }
}

This is very simple Person class, defines the name and age of the two fields in which there is an initial value of an age, name and age of additional definitions set, get method. Very simple, and then we look at this class in a UML class diagram is a representation of how: 

 

 

 We can see that draw a rectangle, and then from top to bottom is divided into three cells, the first cell of the class name, the second field in the class attributes for the grid, where the property is expression of a certain format, as follows:

Permissions Property name: type [= default]
Because here we have the name and age are private, so add one in the front - , in addition to several other rights: public, protected, default, which correspond, respectively + , # , ~ . Since age here has a default value of 1, so that after the type plus '= 1' of FIG. Next, look at the third grid, the third grid is a class method, the following format
Permissions Method name (parameter list) [: return type]

Relations between classes

Well, we have a quick look at the top, a class represented in the class diagram, took us to understand the relationship between class and lower class, class and class relations, a total of the following categories: generalization (Generalization), to achieve ( realization), dependent (dependence), association (association), the polymerization (Aggregation), combination (composition) .

Surely you must have heard such a nursery rhyme: I picked up a penny in the street and then handed over to the police uncle. According to the story then I explain to a relationship between a class and lower class.

Generalization

In Java generalization relationship is also known as inheritance, we use UML in a straight line with a hollow triangle to represent, we add two classes, one class Studen, a police Policemen class, two classes inherit from Person class, their class diagram as follows:

 

 

Realization relationship

Here's realization relationship is to realize the relationship between Java classes and interfaces in the UML, we use hollow triangle with dotted lines. Since the Student and Policeman are professional, vocational student is learning, and the police profession for the protection of the people. All Here we define an interface, which has a professional method:

public interface ICareer{
    void career();
}

 

 We can see more than a name on the interface <<interfac>>character to indicate that the interface. Next we let the Student and Policeman implement this interface, UML class diagram as follows:

 

 

Dependencies

A relationship of dependency is weak, generally refers to a class uses another class, where the students picked up the money over to the police uncle, students and police uncle is a kind of dependency. Because the students picked up the money to the police is an occasional thing, and given to the police no relationship between them. We join in the student class pay a method of adding a method to collect the money in the police class, students pay when calling the method, you call the police method of money. Student code is as follows:

public class Student{
.... .... public Policemen policemen; ..... public void sendCoin(){ policemen.receiveCoin(); } }

Student class can be seen in the referenced class policemen, that is dependent on the Student policemen, this dependence we represented by broken lines with arrows, the arrow pointing to the dependent object, there is Policemen, UML class diagram showing the following:


 

                                                      

connection relation

Relationship is a relatively strong relationship, their relationship is more durable and stable. For example, students from home, student and family is a kind of relationship. This relationship is relatively stable. Sub-way and two-way association association association know if a class or a reference to another class and another class does not know or does not refer to this class, this class is a two-way association. For example, here is the students' relationship with the family of one-way associated with, because every student is to have a home (regardless of orphans), but can not say that every family has the student. We unidirectional association indicated by solid lines with arrows, the arrow points to be included or referenced classes, where the class is at home. Examples are as follows:


 
                                           

Two-way association is both classes know each other's presence, such as the relationship between teachers and students is bidirectional. Xiao Ming is a Chinese teacher Zhang, Zhang Xiaoming of students. Bidirectional association with solid lines without arrows to connect two classes. Examples are as follows:


 
                                 

Aggregation relationship

Aggregation relationship is a special relationship, the relationship between the polymerization and to emphasize the relationship between global portion, wherein the departing portion can be integrally exist. For example, wild geese and geese in a relationship is an aggregation relationship geese leave the geese or may exist independently. Another example is the relationship between the police and the uniforms, uniforms also part of police uniforms from the police may exist. In UML class diagram linear polymeric hollow diamond belt, where the entire diamond point:


 
 

Combination of relationship

Combining relationship is a special relationship, like the relationship between the polymerization it is also emphasized that the overall relationship with the portion different from the entire part can not exist. For example, students Jianqian hand, which is part of the student's hand, but the hand can not exist in isolation from the students. If the hand can exist independently think about the terrible. . . Here we call students with a combination of hand relationship is represented by a straight line with a solid diamond, diamond point where the whole:


 
 

Well, these types of relationship here will almost, in fact, you will find that dependence, association, aggregation, combination of these types of relationship strength is getting stronger: the combination of> Polymerization> association> dependent.

Finally, you will find that you have unknowingly using UML class diagrams completion of the above students Jianqian story:


 
 










 



Guess you like

Origin www.cnblogs.com/newz/p/11909545.html