用latex简单绘制UML图

文章简介

  • 本文是基于pgf-umlcd宏包的简单的UML图绘制方法,使用时须在导言区用命令\usepackage{pgf-umlcd}导入宏包,最好加上simplified参数可以略去一些多余的空格,具体的宏包使用说明,在Windows下的命令行中使用texdoc pgf-umlcd命令查看。用此包绘制出的UML图比较简单,使用起来也比较简单
  • 关于绘制矢量图的最完整的宏包还是tikz宏包,不过这个宏包的说明文档将近1000页,短时间内不太可能绘制一个比较完整的UML图出来。

几段实例代码

  • 需要注意的是在同一个tikzpicture环境中,所有类图共用一个相对坐标,坐标位置设置的不合适的话,可能会使类图重叠。
  • 其中的坐标参数只是相对坐标,并不能表示在纸张上的绝对位置,给出相对坐标后,latex会自动调整整个类图的位置。

类中的数据成员与成员函数

\begin{tikzpicture}		%绘制类图 
    \begin{class}		%类图
        [text width=8cm]%类图的宽度
        {ClassName}		%类名称
        {0,0}			%相对坐标,可以是小数,按数学坐标系,
        \attribute{name:attribute type}								%属性设置,数据成员
        \attribute{name:attribute type = default value}				%属性设置,数据成员
        \operation{name(parameter list):type of value returned}		%函数
        % virtual operation											%虚函数
        \operation[0]                                               %[0]表示这是虚函数
        {name(parameters list):type of value returned }
    \end{class}
    \end{tikzpicture}

效果图:
在这里插入图片描述

类中的成员可见性

\begin{tikzpicture}	
    \begin{class}[text width=8cm]{ClassName}{0,0}
        \attribute{-name : attribute type}							%私有成员
        \attribute{-name : attribute type = default value}			%私有成员
        \operation{+name(parameter list) : type of value returned}	%公有成员
        \operation{\#name : return type}                            %保护成员
        \operation{$\sim$name()  }                                  %析构函数
    \end{class}
\end{tikzpicture}

效果图:在这里插入图片描述

类之间的关系

\begin{tikzpicture}
    \begin{class}[text width =5cm]{BankAccount}{0 ,0}
        \attribute{owner : String }
        \attribute{balance : Dollars = 0}
        \operation{deposit ( amount : Dollars )}
        \operation[0]{ withdrawl ( amount : Dollars )}
    \end{class}
    
    \begin{class}[text width=7cm]{CheckingAccount}{-5,-5}
        \inherit{BankAccount}		%继承,表示此类继承于BankAccount类,自动连线,其它关系的命令如下
        \attribute{insufficientFundsFee : Dollars }
        \operation{processCheck ( checkToProcess : Check)}
        \operation{withdrawal ( amount : Dollars )}
    \end{class}
\end{tikzpicture}

在这段代码中含有表示类之间关系的命令,只要指明了类之间的关系,latex便会自动标注连线,其他命令还有:

  • \implement{} 表明实现关系,用法与\inherit相同
  • 下面的命令不能放在类环境中使用,建议放在tikzpicture环境的最后使用,也就是\end{tikzpicture}之前\end{class}之后。
  • \aggregation{A1}{}{}{A2}表明A1和A2是 聚合 关系,中间两个大括号可以添加标注,可以是空,但必须有大括号
  • \composition{A1}{}{}{A2}表明A1和A2是 组合 关系,用法与聚合相同
  • \unidirectionalAssociation{A1}{}{}{A2}说明A1和A2是 关联关系,用法与聚合相同
    效果图:
    在这里插入图片描述
    几个简单的代码,希望能够帮助到大家,如果发现有什么错误的话,欢迎大家在评论区或者私我指出来,我会及时修改的.
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值