@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
更多注释
可以在注释中使用部分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
@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
WARNING
This translation need to be updated.
WARNING
Note on field (field, attribut, member) or method
It is possible to add a note on field (field, attribut, member) or on method.
Note on field or method
@startuml
class A {
{static} int counter
+void {abstract} start(int timeout)
}
note right of A::counter
This member is annotated
end note
note right of A::start
This method is now explained in a UML note
end note
@enduml
Note on method with the same name
@startuml
class A {
{static} int counter
+void {abstract} start(int timeoutms)
+void {abstract} start(Duration timeout)
}
note left of A::counter
This member is annotated
end note
note right of A::"start(int timeoutms)"
This method with int
end note
note right of A::"start(Duration timeout)"
This method with Duration
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
@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
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
Association on same classe
@startuml
class Station {
+name: string
}
class StationCrossing {
+cost: TimeInterval
}
<> diamond
StationCrossing . diamond
diamond - "from 0..*" Station
diamond - "to 0..* " Station
@enduml
It's possible to declare individual color for classes or note using the # notation. You can use either standard color name or RGB code. You can also use color gradient in background, with the following syntax: two colors names separated either by:
|,
/,
\,
or -
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
辅助布局
有时候,默认布局并不完美... 你可以使用 together 关键词将某些类进行分组: 布局引擎会尝试将它们捆绑在一起(如同在一个包(package)内) 你也可以使用建立 隐藏 链接的方式来强制布局
@startuml
class Bar1
class Bar2
together {
class Together1
class Together2
class Together3
}
Together1 - Together2
Together2 - Together3
Together2 -[hidden]--> Bar1
Bar1 -[hidden]> Bar2
@enduml
You can change the color of individual relation or arrows using the following notation: [#color] or #color;line.[bold|dashed|dotted];text:color:
old method
@startuml
class foo
foo --> bar
foo -[#red]-> bar1
foo -[#green]-> bar2
foo -[#blue]-> bar3
'foo -[#blue;#yellow;#green]-> bar4
@enduml
new method
@startuml
class foo
foo --> bar : normal
foo --> bar1 #line:red;line.bold;text:red : red bold
foo --> bar2 #green;line.dashed;text:green : green dashed
foo --> bar3 #blue;line.dotted;text:blue : blue dotted
@enduml
@startuml
left to right direction
class User {
id : INTEGER
..
other_id : INTEGER
}
class Email {
id : INTEGER
..
user_id : INTEGER
address : INTEGER
}
User::id *-- Email::user_id
@enduml