1、plant UML语法学习小结css
1.类之间的关系html
使用..
来代替 --
能够获得点 线.java
在这些规则下,也能够绘制下列图形shell
@startuml
Class01 <|-- Class02 Class03 *-- Class04 Class05 o-- Class06 Class07 .. Class08 Class09 -- Class10 @enduml
在关系之间使用标签来讲明时, 使用 :
后接 标签文字。ide
对元素的说明,你能够在每一边使用 ""
来讲明.函数
@startuml Class01 "1" *-- "many" Class02 : contains Class03 o-- Class04 : aggregation Class05 --> "1" Class06 @enduml
在标签的开始或结束位置添加 或 以代表是哪一个对象做用到哪一个对象上。<>
@startuml class Car Driver - Car : drives > Car *- Wheel : have 4 > Car -- Person : < owns @enduml
为了声明域或者方法,你可使用 后接域名或方法名。学习
系统检查是否有括号来判断是方法仍是域。字体
@startuml Object <|-- ArrayList Object : equals() ArrayList : Object[] elementData ArrayList : size() @enduml
也可使用 把域或者方法括起来{}
注意,这种语法对于类型/名字的顺序是很是灵活的。this
@startuml class Dummy { String data void methods() } class Flight { flightNumber : Integer departureTime : Date } @enduml
You can use {field}
and {method}
modifiers to override default behaviour of the parser about fields and methods.spa
@startuml class Dummy { {field} A field (despite parentheses) {method} Some method } @enduml
一旦你定义了域或者方法,你能够定义 相应条目的可访问性质。
Character | Icon for field | Icon for method | Visibility |
- |
![]() |
![]() |
private |
# |
![]() |
![]() |
protected |
~ |
![]() |
![]() |
package private |
+ |
![]() |
![]() |
public |
@startuml class Dummy { -field1 #field2 ~method1() +method2() } @enduml
你能够采用如下命令停用这些特性 skinparam classAttributeIconSize 0
:
@startuml skinparam classAttributeIconSize 0 class Dummy { -field1 #field2 ~method1() +method2() } @enduml
经过修饰符{static}
或者{abstract}
,能够定义静态或者抽象的方法或者属性。
这些修饰符能够写在行的开始或者结束。也可使用{classifier}
这个修饰符来代替{static}
.
@startuml class Dummy { {static} String id {abstract} void methods() } @enduml
PlantUML默认自动将方法和属性从新分组,你能够本身定义分隔符来重排方法和属性,下面的分隔符都是可用的:--
..
==
__
.
还能够在分隔符中添加标题:
@startuml class Foo1 { You can use several lines .. as you want and group == things together. __ You can have as many groups as you want -- End of class } class User { .. Simple Getter .. + getName() + getAddress() .. Some setter .. + setName() __ private data __ int age -- encrypted -- String password } @enduml
模板经过类关键字("<<"和">>")来定义
你可使用note left of
, note right of
, note top of
, note bottom of
这些关键字来添加备注。
你还能够在类的声明末尾使用note left
, note right
,note top
, note bottom
来添加备注。
此外,单独用note
这个关键字也是能够的,使用 ..
符号能够做出一条链接它与其它对象的虚线。
@startuml class Object << general >> Object <|--- ArrayList note top of Object : In java, every class\nextends this one. note "This is a floating note" as N1 note "This note is connected\nto several objects." as N2 Object .. N2 N2 .. ArrayList class Foo note left: On last defined class @enduml
8.更多注释
能够在注释中使用部分html标签:
<b>
<u>
<i>
<s>
, <del>
, <strike>
<font color="#AAAAAA">
or <font color="colorName">
<color:#AAAAAA>
or <color:colorName>
<size:nn>
to change font size<img src="file">
or <img:file>
: the file must be accessible by the filesystem你也能够在注释中展现多行。
你也能够在定义的class以后直接使用 note left
, note right
, note top
, note bottom
来定义注释。
@startuml class Foo note left: On last defined class note top of Object In java, <size:18>every</size> <u>class</u> <b>extends</b> <i>this</i> one. end note note as N1 This note is <u>also</u> <b><color:royalBlue>on several</color> <s>words</s> lines And this is hosted by <img:sourceforge.jpg> end note @enduml
在定义连接以后,你能够用 note on link
给连接添加注释
若是想要改变注释相对于标签的位置,你也能够用 note left on link
, note right on link
, note bottom on link
。(对应位置分别在label的左边,右边,下边)
@startuml class Dummy Dummy --> Foo : A link note on link #red: note that is red Dummy --> Foo2 : Another link note right on link #blue this is my note on right link and in blue end note @enduml
用关键字abstract
或abstract class
来定义抽象类。抽象类用斜体显示。 也可使用interface
, annotation
和 enum
关键字。
@startuml abstract class AbstractList abstract AbstractCollection interface List interface Collection List <|-- AbstractList Collection <|-- AbstractCollection Collection <|- List AbstractCollection <|- AbstractList AbstractList <|-- ArrayList class ArrayList { Object[] elementData size() } enum TimeUnit { DAYS HOURS MINUTES } annotation SuppressWarnings @enduml
若是你想在类(或者枚举)的显示中使用非字母符号,你能够:
as
关键字""
@startuml class "This is my class" as class1 class class2 as "It works this way too" class2 *-- "foo/dummy" : use @enduml
经过使用命令“hide/show
”,你能够用参数表示类的显示方式。
基础命令是: hide empty members
. 这个命令会隐藏空白的方法和属性。
除 empty members
外,你能够用:
empty fields
或者 empty attributes
空属性,empty methods
空函数,fields
或 attributes
隐藏字段或属性,即便是被定义了methods
隐藏方法,即便是被定义了members
隐藏字段 和 方法,即便是被定义了circle
类名前带圈的,stereotype
原型。一样可使用 hide
或 show
关键词,对如下内容进行设置:
class
全部类,interface
全部接口,enum
全部枚举,<<foo1>>
实现 foo1 的类,你可使用 show/hide
命令来定义相关规则和例外。
@startuml class Dummy1 { +myMethods() } class Dummy2 { +hiddenMethod() } class Dummy3 <<Serializable>> { String name } hide members hide <<Serializable>> circle show Dummy1 methods show <<Serializable>> fields @enduml
你也可使用 show/hide
命令来隐藏类
若是你定义了一个大的!included 文件,且想在文件包含以后隐藏部分类,该功能会颇有帮助。
@startuml class Foo1 class Foo2 Foo2 *-- Foo1 hide Foo2 @enduml
你能够用 <
和 >
来定义类的泛型。
@startuml class Foo<? extends Element> { int size() } Foo *- Element @enduml
It is possible to disable this drawing using skinparam genericDisplay old
command.
你能够经过关键词 package
声明包,同时可选的来声明对应的背景色(经过使用html色彩代码或名称)。
注意:包能够被定义为嵌套。
@startuml package "Classic Collections" #DDDDDD { Object <|-- ArrayList } package net.sourceforge.plantuml { Object <|-- Demo1 Demo1 *- Demo2 } @enduml
包能够定义不一样的样式。
你能够经过如下的命令来设置默认样式 : skinparam packageStyle
,或者对包使用对应的模板:
@startuml scale 750 width package foo1 <<Node>> { class Class1 } package foo2 <<Rectangle>> { class Class2 } package foo3 <<Folder>> { class Class3 } package foo4 <<Frame>> { class Class4 } package foo5 <<Cloud>> { class Class5 } package foo6 <<Database>> { class Class6 } @enduml
你也能够参考下面的示例来定义包之间的连线:
@startuml skinparam packageStyle rectangle package foo1.foo2 { } package foo1.foo2.foo3 { class Object } foo1.foo2 +-- foo1.foo2.foo3 @enduml
In packages, the name of a class is the unique identifier of this class. It means that you cannot have two classes with the very same name in different packages.
In that case, you should use namespacesinstead of packages.
You can refer to classes from other namespaces by fully qualify them. Classes from the default namespace are qualified with a starting dot.
Note that you don't have to explicitly create namespace : a fully qualified class is automatically put in the right namespace.
@startuml class BaseClass namespace net.dummy #DDDDDD { .BaseClass <|-- Person Meeting o-- Person .BaseClass <|- Meeting } namespace net.foo { net.dummy.Person <|- Person .BaseClass <|-- Person net.dummy.Meeting o-- Person } BaseClass <|-- net.unused.Person @enduml
18.自动建立命名空间
使用命令 set namespaceSeparator ???
你能够自定义命名空间分隔符(为 “.” 之外的字符).
@startuml set namespaceSeparator :: class X1::X2::foo { some info } @enduml
禁止自动建立包则可使用 set namespaceSeparator none
.
@startuml set namespaceSeparator none class X1.X2.foo { some info } @enduml
须要定义棒棒糖样式的接口时能够遵循如下语法:
bar ()- foo
bar ()-- foo
foo -() bar
@startuml class foo bar ()- foo @enduml
类之间默认采用两个破折号 --
显示出垂直 方向的线. 要获得水平方向的能够像这样使用单破折号 (或者点):
@startuml Room o- Student Room *-- Chair @enduml
你也能够经过改变倒置连接来改变方向
@startuml Student -o Room Chair --* Room @enduml
也可经过在箭头内部使用关键字, 例如left
, right
, up
或者 down
,来改变方向
@startuml foo -left-> dummyLeft foo -right-> dummyRight foo -up-> dummyUp foo -down-> dummyDown @enduml
You can shorten the arrow by using only the first character of the direction (for example, -d-
instead of -down-
) or the two first characters (-do-
).
Please note that you should not abuse this functionality : Graphviz gives usually good results without tweaking.
你能够在定义了两个类之间的关系后定义一个 关系类 association class 例如:
@startuml class Student { Name } Student "0..*" - "1..*" Course (Student, Course) .. Enrollment class Enrollment { drop() cancel() } @enduml
也能够用另外一种方式:
@startuml class Student { Name } Student "0..*" -- "1..*" Course (Student, Course) . Enrollment class Enrollment { drop() cancel() } @enduml
用skinparam
改变字体和颜色。
能够在以下场景中使用:
@startuml skinparam class { BackgroundColor PaleGreen ArrowColor SeaGreen BorderColor SpringGreen } skinparam stereotypeCBackgroundColor YellowGreen Class01 "1" *-- "many" Class02 : contains Class03 o-- Class04 : aggregation @enduml
You can define specific color and fonts for stereotyped classes.
@startuml skinparam class { BackgroundColor PaleGreen ArrowColor SeaGreen BorderColor SpringGreen BackgroundColor<<Foo>> Wheat BorderColor<<Foo>> Tomato } skinparam stereotypeCBackgroundColor YellowGreen skinparam stereotypeCBackgroundColor<< Foo >> DimGray Class01 <<Foo>> Class03 <<Foo>> Class01 "1" *-- "many" Class02 : contains Class03 o-- Class04 : aggregation @enduml
It's possible to declare individual color for classes or note using the # notation.
You can use either standard color nameor RGB code.
You can also use color gradient in background, with the following syntax: two colors names separated either by:
|
,/
,\
,-
depending the direction of the gradient.
For example, you could have:
@startuml skinparam backgroundcolor AntiqueWhite/Gold skinparam classBackgroundColor Wheat|CornflowerBlue class Foo #red-green note left of Foo #blue\9932CC this is my note on this class end note package example #GreenYellow/LightGoldenRodYellow { class Dummy } @enduml
Sometimes, the default layout is not perfect...
You can use together
keyword to group some classes together : the layout engine will try to group them (as if they were in the same package).
You can also use hidden
links to force the layout.
@startuml class Bar1 class Bar2 together { class Together1 class Together2 class Together3 } Together1 - Together2 Together2 - Together3 Together2 -[hidden]--> Bar1 Bar1 -[hidden]> Bar2 @enduml
有些状况下,会有一些很大的图片文件。
能够用 page (hpages)x(vpages)
这个命令把生成的图片文件拆分红若干个文件。
hpages
用来表示水平方向页面数, and vpages
用来表示垂直方面页面数。
你也可使用特定的皮肤设定来给分页添加边框(见例子)
@startuml ' Split into 4 pages page 2x2 skinparam pageMargin 10 skinparam pageExternalColor gray skinparam pageBorderColor black class BaseClass namespace net.dummy #DDDDDD { .BaseClass <|-- Person Meeting o-- Person .BaseClass <|- Meeting } namespace net.foo { net.dummy.Person <|- Person .BaseClass <|-- Person net.dummy.Meeting o-- Person } BaseClass <|-- net.unused.Person @enduml
属性:
学生基本信息:学号,姓名,性别 , 班级 ,
学生课程信息:课程号 , 课程名 , 成绩 ,
学生选修课信息:学号,课程号,先修课。
3、类图脚本程序
@startuml
class 学生基本信息 {
姓名:string
学号:varchar
性别:string
班级:string
__
+添加信息
+修改信息
+删除信息
+更新信息
+保存
}
class 学生课程信息{
课程号:varchar
课程名:string
成绩:string
__
+查看课程
+课程成绩
}
class 学生选修课信息 {
学号:varchar
课程号:varchar
先修课:string
__
+登陆
+查找先修课
+退出
}
学生课程信息 <--> 学生基本信息
学生选修课信息 <--> 学生基本信息
@enduml
4、类图示例