使用PlantUml绘制类图1
1. 类之间的关系1
Class02 继承 Class01 ,箭头朝上。
Class03 和 Class04 的关系是组合,Class04 是组成 Class03 的一部分。
Class05 和 Class06 的关系是聚合,是整体与部分的关系,部分可脱离整体而单独存在,
表示符号:带空心菱形的实线,菱形指向整体。
Class09 和 Class10 的关系是关联,双向关联箭头可有可无。
@startuml
title 类之间的关系
Class01 <|-- Class02
note left of Class02 : Class02继承Class01,箭头朝上
Class12 --|> Class11
note left of Class12 : Class12继承Class11,箭头朝下
Class03 *-- Class04
note "Class03和Class04的关系是组合,Class04是组成Class03的一部分" as N1
Class03 .. N1
N1 .. Class04
Class05 o-- Class06
note left
Class05和Class06的关系是聚合,
Class06聚成了Class05,Class06也可单独存在
end note
Class07 .. Class08
Class09 -- Class10
note "Class09和Class10的关系是关联,双向关联的箭头可有可无" as N2
Class09 .. N2
N2 .. Class10
@enduml2. 类之间的关系2
Class12 是 Class11 的实现。
Class13 与 Class14 是一种关联关系,是一种拥有关系 (has a)
表示符号:带普通箭头的实线,指向被拥有者, 双向关联箭头可有可无。
Class15 与 Class16 是一种依赖关系,是一种使用的关系。一个类的实现需要另一个类的协助。
表示符号:带箭头的虚线,指向被使用者。
代码体现:被使用者可以是的局部变量、方法的参数或静态方法的调用。
Class19 与 Class20 是一种关联关系,Class19 是 Class20 的不可分割的一部分。
@startuml
Class11 <|.. Class12
note left: 实现关系
Class13 --> Class14
note left of Class13: 关联关系
Class15 ..> Class16
note left of Class15: 依赖关系
Class17 ..|> Class18
note left: 实现关系
Class19 <--* Class20
note left: 组合、关联关系
@enduml3. 类关系上的标识1
在关系之间使用标签来说明时, 使用 : 后接标签文字。
对元素的说明,你可以在每一边使用 "" 来说明.
@startuml
title 关系上的标识
Class01 "1" *-- "many" Class02 : 组合
Class03 o-- Class04 : 聚合
Class05 --> "1" Class06
@enduml4. 类关系上的标识2
在标签的开始或结束位置添加 < 或 > 以表明是哪个对象作用到哪个对象上。
@startuml
Driver - Car : drives >
Car *- Wheel : have 4 >
Car -- Person : < owns
@enduml5. 为类添加字段和方法
为了声明字段或者方法,你可以使用后接字段名或方法名,比如 ArrayList类。
系统检查是否有括号来判断是方法还是字段。
也可以使用 {} 把字段或者方法括起来,比如 Dummy类。
注意,这种语法对于类型/名字的顺序是非常灵活的。
你可以(显式地)使用 {field} 和 {method} 修饰符来覆盖解析器的对于字段和方法的默认行为。
比如 Dummy1
@startuml
Object <|-- ArrayList
Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
class Dummy {
String data
void methods()
}
class Flight {
flightNumber : Integer
departureTime : Date
}
class Dummy1 {
{field} A field
{method} Some method
}
@enduml6. 类的修饰符
@startuml
' skinparam classAttributeIconSize 0
class Dummy {
' 私有
-field1
' 保护
#field2
' 包私有
~field3
' 公有
+field4
-method1()
#method2()
~method3()
+method4()
}
@enduml7. 抽象与静态
通过修饰符{static}或者 {abstract} ,可以定义静态或者抽象的方法或者属性。
这些修饰符可以写在行的开始或者结束。也可以使用 {classifier} 这个修饰符来代替 {static}.
@startuml
class Dummy {
{static} String id
{abstract} void methods()
}
@enduml8. 高级类体
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
}
@enduml9. 备注和模板
模板通过类关键字 (<< 和 >>) 来定义
你可以使用 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
@enduml10. 高级的备注
可以在注释中使用部分 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:test2.png>
end note
@enduml11. 链接的备注
在定义链接之后,你可以用 note on link 给链接添加注释
如果想要改变注释相对于标签的位置,你也可以用:note left on link, note right on link,note bottom on link。
(对应位置分别在 label 的左边,右边,下边)
@startuml
class Dummy
Dummy --> Foo : 链接1
note on link #red: 红色的备注
Dummy --> Foo2 : 链接2
note right on link #blue
在右边的备注
蓝色的备注
end note
@enduml12. 抽象类和接口
用关键字 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
@enduml13. 使用非字母字符
如果你想在类(或者枚举)的显示中使用非字母符号,你可以:
• 在类的定义中使用 as 关键字
• 在类名旁边加上 ""
@startuml
class "This is my class" as class1
class class2 as "It works this way too"
class2 *-- "foo/dummy" : use
@enduml