plantuml样式_PlantUML类图的写法
PlantUML类图
PlantUML⽤下⾯的符号来表⽰类之间的关系:类之间的关系
泛化, Generalization:
关联, Association:
组合, Composition: *--
聚合, Aggregation: o--
实现, Realization:
依赖, Dependency: <..>
以上是常见的六种关系, --可以替换成 ..就可以得到虚线。另外,其中的符号是可以改变⽅向的,例如: 表⽰左边的类泛化右边的类。例如,下⾯的是 --:
@startuml
Class01
Class03
Class05 *-- Class06:组合
Class07 o-- Class08:聚合
Class09 -- Class10
@enduml
⽣成的类图如下:
--可以替换成 ..,对应的虚线:
@startuml
Class11
Class13 <.. class14:>
Class15 *.. Class16
Class17 o.. Class18
Class19 .. Class20
@enduml
⽣成的类图如下:
关系上的标签
可以在关系上添加标签,只需要在⽂本后⾯添加冒号和标签名称即可。可以在关联的两边使⽤双引号。例如:
@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
⽣成的类图如下:
上⾯的类图意思是:
Driver 驾驶 Car
Car 有4个 Wheel
Person 拥有 Car
添加⽅法
在类名后⾯添加冒号可以添加⽅法和⽅法的参数,例如:@startuml
Object
Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
@enduml
⽣成的类图如下:
也可以使⽤{}来定义所有的字段及字段和⽅法,例如:
@startuml
class Dummy {
String data
void methods()
}
class Flight {
flightNumber : Integer
departureTime : Date
}
@enduml
⽣成的类图如下:
定义可见性
以下符号定义字段或者⽅法的可见性:
-: private
#: protected
~
: package private
+: public
例如:
@startuml
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml
你可以使⽤skinparam classAttributeIconSize 0关掉icon的显⽰:
@startuml
skinparam classAttributeIconSize 0
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml
抽象和静态
你可以使⽤ {static}或者 {abstract}来修饰字段或者⽅法,修饰符需要在⾏的开头或者末尾使⽤。你也可以使⽤ {classifier}代替 {static}。
@startuml
class Dummy {
{static} String id
{classifier} String name
{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 classes
}
class User {
.. Simple Getter ..
+ getName()
+ getAddress()
.. Some setter ..
+ setName()
__ private data __
int age
-- encrypted --
String password
}
@enduml
注释和原型
原型使⽤ class、 <>进⾏定义。
注释使⽤ note
left of、 note
right of、 note
top of、 note
bottom of关键字进⾏定义。
你也可以在最后⼀个定义的类上使⽤ note
left、 note
right、 note
top、 note
bottom关键字。
注释可以使⽤ ..与其他对象进⾏连接。
@startuml
class Object << general >>
Object
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标签进⾏修饰:
, ,
或者
或者