文章简介
- 本文是基于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是 关联关系,用法与聚合相同
效果图:
几个简单的代码,希望能够帮助到大家,如果发现有什么错误的话,欢迎大家在评论区或者私我指出来,我会及时修改的.