您当前的位置: 首页 > 

Bulut0907

暂无认证

  • 5浏览

    0关注

    346博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

UML类图(简介、类的表示、对象的表示、关系、注释)

Bulut0907 发布时间:2021-01-08 12:10:25 ,浏览量:5

目录
  • 1. 简介
  • 2. 类和对象
    • 2.1 类的表示
    • 2.2 对象的表示
  • 3. 关系的表示
    • 3.1 依赖关系
    • 3.2 作用关系---关联
    • 3.3 包含关系---聚集和组合
    • 3.4 继承关系-泛化
  • 4. 注释

1. 简介

UML(Unified Modeling Language)是一种面向对象的建模语言,包含我们所需要的所有图形标识;是一种更直观的图形方式,用符号来表示概念,概念间的关系用连接符合的线来表示

UML的各种图形,分别描述软件模型的静态结构、动态行为、模块组织和管理;本文我们讲解的类图(class diagram),是一种静态结构图(static structrue diagrams),用来描述类和对象、类和对象的内部结构、类和对象的关系

2. 类和对象 2.1 类的表示

我们先来看一个简单的例子,体验一下

class Student {
public:
    Student(string n = "",int a=0) {
        name=n;
        age= a;
    }

    void show() {
        cout tspan{fill:#000;stroke:none}#mermaid-svg-iaJP9tyxPo5HeNEg .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-iaJP9tyxPo5HeNEg .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-iaJP9tyxPo5HeNEg .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-iaJP9tyxPo5HeNEg .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iaJP9tyxPo5HeNEg .section{stroke:none;opacity:0.2}#mermaid-svg-iaJP9tyxPo5HeNEg .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-iaJP9tyxPo5HeNEg .section2{fill:#fff400}#mermaid-svg-iaJP9tyxPo5HeNEg .section1,#mermaid-svg-iaJP9tyxPo5HeNEg .section3{fill:#fff;opacity:0.2}#mermaid-svg-iaJP9tyxPo5HeNEg .sectionTitle0{fill:#333}#mermaid-svg-iaJP9tyxPo5HeNEg .sectionTitle1{fill:#333}#mermaid-svg-iaJP9tyxPo5HeNEg .sectionTitle2{fill:#333}#mermaid-svg-iaJP9tyxPo5HeNEg .sectionTitle3{fill:#333}#mermaid-svg-iaJP9tyxPo5HeNEg .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iaJP9tyxPo5HeNEg .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-iaJP9tyxPo5HeNEg .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iaJP9tyxPo5HeNEg .grid path{stroke-width:0}#mermaid-svg-iaJP9tyxPo5HeNEg .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-iaJP9tyxPo5HeNEg .task{stroke-width:2}#mermaid-svg-iaJP9tyxPo5HeNEg .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iaJP9tyxPo5HeNEg .taskText:not([font-size]){font-size:11px}#mermaid-svg-iaJP9tyxPo5HeNEg .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iaJP9tyxPo5HeNEg .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-iaJP9tyxPo5HeNEg .task.clickable{cursor:pointer}#mermaid-svg-iaJP9tyxPo5HeNEg .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-iaJP9tyxPo5HeNEg .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-iaJP9tyxPo5HeNEg .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-iaJP9tyxPo5HeNEg .taskText0,#mermaid-svg-iaJP9tyxPo5HeNEg .taskText1,#mermaid-svg-iaJP9tyxPo5HeNEg .taskText2,#mermaid-svg-iaJP9tyxPo5HeNEg .taskText3{fill:#fff}#mermaid-svg-iaJP9tyxPo5HeNEg .task0,#mermaid-svg-iaJP9tyxPo5HeNEg .task1,#mermaid-svg-iaJP9tyxPo5HeNEg .task2,#mermaid-svg-iaJP9tyxPo5HeNEg .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-iaJP9tyxPo5HeNEg .taskTextOutside0,#mermaid-svg-iaJP9tyxPo5HeNEg .taskTextOutside2{fill:#000}#mermaid-svg-iaJP9tyxPo5HeNEg .taskTextOutside1,#mermaid-svg-iaJP9tyxPo5HeNEg .taskTextOutside3{fill:#000}#mermaid-svg-iaJP9tyxPo5HeNEg .active0,#mermaid-svg-iaJP9tyxPo5HeNEg .active1,#mermaid-svg-iaJP9tyxPo5HeNEg .active2,#mermaid-svg-iaJP9tyxPo5HeNEg .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-iaJP9tyxPo5HeNEg .activeText0,#mermaid-svg-iaJP9tyxPo5HeNEg .activeText1,#mermaid-svg-iaJP9tyxPo5HeNEg .activeText2,#mermaid-svg-iaJP9tyxPo5HeNEg .activeText3{fill:#000 !important}#mermaid-svg-iaJP9tyxPo5HeNEg .done0,#mermaid-svg-iaJP9tyxPo5HeNEg .done1,#mermaid-svg-iaJP9tyxPo5HeNEg .done2,#mermaid-svg-iaJP9tyxPo5HeNEg .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-iaJP9tyxPo5HeNEg .doneText0,#mermaid-svg-iaJP9tyxPo5HeNEg .doneText1,#mermaid-svg-iaJP9tyxPo5HeNEg .doneText2,#mermaid-svg-iaJP9tyxPo5HeNEg .doneText3{fill:#000 !important}#mermaid-svg-iaJP9tyxPo5HeNEg .crit0,#mermaid-svg-iaJP9tyxPo5HeNEg .crit1,#mermaid-svg-iaJP9tyxPo5HeNEg .crit2,#mermaid-svg-iaJP9tyxPo5HeNEg .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-iaJP9tyxPo5HeNEg .activeCrit0,#mermaid-svg-iaJP9tyxPo5HeNEg .activeCrit1,#mermaid-svg-iaJP9tyxPo5HeNEg .activeCrit2,#mermaid-svg-iaJP9tyxPo5HeNEg .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-iaJP9tyxPo5HeNEg .doneCrit0,#mermaid-svg-iaJP9tyxPo5HeNEg .doneCrit1,#mermaid-svg-iaJP9tyxPo5HeNEg .doneCrit2,#mermaid-svg-iaJP9tyxPo5HeNEg .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-iaJP9tyxPo5HeNEg .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-iaJP9tyxPo5HeNEg .milestoneText{font-style:italic}#mermaid-svg-iaJP9tyxPo5HeNEg .doneCritText0,#mermaid-svg-iaJP9tyxPo5HeNEg .doneCritText1,#mermaid-svg-iaJP9tyxPo5HeNEg .doneCritText2,#mermaid-svg-iaJP9tyxPo5HeNEg .doneCritText3{fill:#000 !important}#mermaid-svg-iaJP9tyxPo5HeNEg .activeCritText0,#mermaid-svg-iaJP9tyxPo5HeNEg .activeCritText1,#mermaid-svg-iaJP9tyxPo5HeNEg .activeCritText2,#mermaid-svg-iaJP9tyxPo5HeNEg .activeCritText3{fill:#000 !important}#mermaid-svg-iaJP9tyxPo5HeNEg .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iaJP9tyxPo5HeNEg g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-iaJP9tyxPo5HeNEg g.classGroup text .title{font-weight:bolder}#mermaid-svg-iaJP9tyxPo5HeNEg g.clickable{cursor:pointer}#mermaid-svg-iaJP9tyxPo5HeNEg g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-iaJP9tyxPo5HeNEg g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-iaJP9tyxPo5HeNEg .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-iaJP9tyxPo5HeNEg .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-iaJP9tyxPo5HeNEg .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-iaJP9tyxPo5HeNEg .dashed-line{stroke-dasharray:3}#mermaid-svg-iaJP9tyxPo5HeNEg #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-iaJP9tyxPo5HeNEg #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-iaJP9tyxPo5HeNEg #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-iaJP9tyxPo5HeNEg #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-iaJP9tyxPo5HeNEg #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-iaJP9tyxPo5HeNEg #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-iaJP9tyxPo5HeNEg #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-iaJP9tyxPo5HeNEg #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-iaJP9tyxPo5HeNEg .commit-id,#mermaid-svg-iaJP9tyxPo5HeNEg .commit-msg,#mermaid-svg-iaJP9tyxPo5HeNEg .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iaJP9tyxPo5HeNEg .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iaJP9tyxPo5HeNEg .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iaJP9tyxPo5HeNEg g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iaJP9tyxPo5HeNEg g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-iaJP9tyxPo5HeNEg g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-iaJP9tyxPo5HeNEg g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-iaJP9tyxPo5HeNEg g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-iaJP9tyxPo5HeNEg g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-iaJP9tyxPo5HeNEg .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-iaJP9tyxPo5HeNEg .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-iaJP9tyxPo5HeNEg .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-iaJP9tyxPo5HeNEg .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-iaJP9tyxPo5HeNEg .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-iaJP9tyxPo5HeNEg .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-iaJP9tyxPo5HeNEg .edgeLabel text{fill:#333}#mermaid-svg-iaJP9tyxPo5HeNEg .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-iaJP9tyxPo5HeNEg .node circle.state-start{fill:black;stroke:black}#mermaid-svg-iaJP9tyxPo5HeNEg .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-iaJP9tyxPo5HeNEg #statediagram-barbEnd{fill:#9370db}#mermaid-svg-iaJP9tyxPo5HeNEg .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-iaJP9tyxPo5HeNEg .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-iaJP9tyxPo5HeNEg .statediagram-state .divider{stroke:#9370db}#mermaid-svg-iaJP9tyxPo5HeNEg .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-iaJP9tyxPo5HeNEg .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-iaJP9tyxPo5HeNEg .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-iaJP9tyxPo5HeNEg .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-iaJP9tyxPo5HeNEg .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-iaJP9tyxPo5HeNEg .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-iaJP9tyxPo5HeNEg .note-edge{stroke-dasharray:5}#mermaid-svg-iaJP9tyxPo5HeNEg .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-iaJP9tyxPo5HeNEg .error-icon{fill:#522}#mermaid-svg-iaJP9tyxPo5HeNEg .error-text{fill:#522;stroke:#522}#mermaid-svg-iaJP9tyxPo5HeNEg .edge-thickness-normal{stroke-width:2px}#mermaid-svg-iaJP9tyxPo5HeNEg .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-iaJP9tyxPo5HeNEg .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-iaJP9tyxPo5HeNEg .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-iaJP9tyxPo5HeNEg .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-iaJP9tyxPo5HeNEg .marker{fill:#333}#mermaid-svg-iaJP9tyxPo5HeNEg .marker.cross{stroke:#333}

:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
  #mermaid-svg-iaJP9tyxPo5HeNEg {
    color: rgba(0, 0, 0, 0.75);
    font: ;
  }
  
  
   
    
   
  
  
   
    
   
  
  
   
    
   
  
  
   
    
   
  
  
   
    
   
  
  
   
    
   
  
  
   
    
   
  
  
   
    
   
  
  
   
   
    
     Student
    
   
   
   
   
   
  
 
  • 数据成员的表示
    • 语法: [访问控制属性]名称 [重数] [: 类型] [= 默认值] [{约束特征}]
    • 访问控制属性:public、private、protected分布对应UML的"+"、"-"、"#"
    • 名称:数据成员的变量名
    • 重数:属性的重数,具体什么意思没搞懂
    • 默认值:如该数据成员的类型为AreaArea类构造函数为Area(int length, int width),则该数据成员的默认值形式为= (10, 10)
    • 约束特征:该数据成员性质约束的说明,如{只读}表示该数据成员只能被读取
2.2 对象的表示

我们用上面的类Student创建一个对象my_student 对象图效果 说明:

  • 分为两部分,上面部分为对象名,下面部分为数据成员及其值,数据成员及其值是可选的
  • 对象名格式为:对象名 : 类名,下面要加下划线,可以省略对象名或类名
3. 关系的表示

使用带有特定符号的实线段或虚线段表示关系

3.1 依赖关系

表示一个类A使用另一个类B作为函数的成员参数,就要用依赖关系,通常为类之间的调用、友元和类的实例化;B变化会影响A,反之不成立

UML中表示依赖关系使用构造型,如下所示: 依赖关系图 表示类A使用了类B

3.2 作用关系—关联

表示一个类的对象和另一个类的对象之间相互作用的关系,UML中用实线来表示两个类(或同一个类)之间的关联 关联关系图 重数A表示:类B的每个对象与类A的多少个对象发生作用,如:重数A = 0..1 重数B表示:类A的每个对象与类B的多少个对象发生作用

重数标记的形式和含义如下表所示:

标记说明*任何数目的对象(包括0)1恰好1个对象n恰好n个对象0…10个或1个对象(表明关联是可选的)n…m最少为n个对象, 最多为m个对象(n和m是整数)2,4离散的结合(如2个或4个) 3.3 包含关系—聚集和组合

聚集表示整体与部分的关系,是一种特殊的关联,聚集分为共享聚集和组成聚集(组合),如下所示: 包含关系图

  • 左图表示共享聚合:类A包含多个类B,但类B还可以被其它类包含,在UML中由空心菱形表示
  • 右图表示组成聚合(组合):类A包含多个类B,且类B只能被类A包含,在UML中由实心菱形表示
3.4 继承关系-泛化

类的继承关系在UML中称为泛化,如下图所示,子类1说明单继承,子类2说明多继承 继承关系图

4. 注释

UML的注释是一种能独立存在的修饰符号,附加在元素或元素集上用来表示说明的图形符号,可以添加说明、评述、注解等信息,如下所示: 注释图 其中虚线连接到UML的其它元素上

关注
打赏
1664501120
查看更多评论
0.1720s