首页 技术 正文
技术 2022年11月20日
0 收藏 483 点赞 4,538 浏览 6775 个字

一、类图

(1)类图定义

类图,是UML(统一建模语言)中用于描述”类”以及”类与类”之间的示意图。它形象的描述出了系统的结构,帮助人们理解系统。 类图是在”所有的UML图”中,实用频率非常之高;掌握它对于我们软件设计,以及交流都很有帮助。对于类图而言,它的基本单位是类。类主要由三部分组成:类名、属性、操作(函数)。

类名:类的名称

属性:UML类图中,属性的基本格式: 可见性 名称: 类型 [=缺省值]

操作:UML类图中,属性的基本格式: 可见性 名称(参数类表) [:返回类型]

(2)类之间的关系

类之间的符号定义

UML与软件建模:第二次作业(类图中类的表示)

UML与软件建模:第二次作业(类图中类的表示)

@startuml

C01 <|– C02:泛化

C03 <– C04:关联

C05 *– C06:组合

C07 o– C08:聚合

C09 <|.. C10:实现

C11 <.. C12:依赖

C13 — C14

@enduml

以上是常见的六种关系, -- 可以替换成 .. 就可以得到虚线。另外,其中的符号是可以改变方向的,例如: <|-- 表示右边的类泛化左边的类; --|> 表示左边的类泛化右边的类。

(1)关系上的标签

可以在关系上添加标签,只需要在文本后面添加冒号和标签名称即可。可以在关联的两边使用双引号。例如:

@startuml
C01 “1” *– “many” C02 : contains
C03 o– C04 : aggregation
Cs05 –> “1” C06
@enduml

UML与软件建模:第二次作业(类图中类的表示)

可以在关系上使用 或者 表名两个类之间的关系,例如:

@startuml

class Car

Driver – Car:drives >

Car*-Wheel:have 4>

Car –Person:<owns

@enduml

UML与软件建模:第二次作业(类图中类的表示)

(2)添加方法

在类名后面添加冒号可以添加方法和方法的参数,例如:

@startuml
Object <|– ArrayList
Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
@enduml

UML与软件建模:第二次作业(类图中类的表示)

也可以使用{}来定义所有的字段及字段和方法,例如:

@startuml class Dummy {

String data   void methods()

}

class Flight {

flightNumber : Integer

departureTime : Date

}

@enduml

UML与软件建模:第二次作业(类图中类的表示)

(3)定义可访问性

UML与软件建模:第二次作业(类图中类的表示)

@startuml

class Dummy {
-field1
#field2
~method1()
+method2()
}

@enduml

UML与软件建模:第二次作业(类图中类的表示)

可以采用以下命令停用这些特性 skinparam classAttributeIconSize 0

@startuml
skinparam classAttributeIconSize 0
class Dummy {
-field1
#field2
~method1()
+method2()
}@enduml

UML与软件建模:第二次作业(类图中类的表示)

(4)抽象和静态

可以使用 {static} 或者 {abstract} 来修饰字段或者方法,修饰符需要在行的开头或者末尾使用。你也可以使用 {classifier} 代替 {static} 

@startuml

class Dunmy{

{static}String id

{classifier}String name

{abstract}void methods()

}

@enduml

UML与软件建模:第二次作业(类图中类的表示)

(5)高级类体

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

UML与软件建模:第二次作业(类图中类的表示)

(6)备注和模板

模板通过类关键字(“<<“和”>>”)来定义

你可以使用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 <|--- ArrayListnote 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 .. ArrayListclass Foo
note left: On last defined class@enduml

UML与软件建模:第二次作业(类图中类的表示)

(7)更多注释
可以在注释中使用部分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 来定义注释。

@startumlclass Foo
note left: On last defined classnote top of Object
In java, <size:18>every</size> <u>class</u>
<b>extends</b>
<i>this</i> one.
end notenote 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

UML与软件建模:第二次作业(类图中类的表示)

(8)链接的注释
在定义链接之后,你可以用 note on link 给链接添加注释

如果想要改变注释相对于标签的位置,你也可以用 note left on linknote right on linknote bottom on link。(对应位置分别在label的左边,右边,下边)

@startumlclass Dummy
Dummy --> Foo : A link
note on link #red: note that is redDummy --> Foo2 : Another link
note right on link #blue
this is my note on right link
and in blue
end note@enduml

UML与软件建模:第二次作业(类图中类的表示)

(9)抽象类和接口
用关键字abstractabstract class来定义抽象类。抽象类用斜体显示。 也可以使用interface, annotationenum关键字。
@startumlabstract class AbstractList
abstract AbstractCollection
interface List
interface CollectionList <|-- AbstractList
Collection <|-- AbstractCollectionCollection <|- List
AbstractCollection <|- AbstractList
AbstractList <|-- ArrayListclass ArrayList {
Object[] elementData
size()
}enum TimeUnit {
DAYS
HOURS
MINUTES
}annotation SuppressWarnings@enduml

UML与软件建模:第二次作业(类图中类的表示)

(10)使用非字母字符
如果你想在类(或者枚举)的显示中使用非字母符号,你可以:
  • 在类的定义中使用 as 关键字
  • 在类名旁边加上 ""
@startuml
class "This is my class" as class1
class class2 as "It works this way too"class2 *-- "foo/dummy" : use
@enduml

UML与软件建模:第二次作业(类图中类的表示)

(11)隐藏属性、函数等
通过使用命令“hide/show”,你可以用参数表示类的显示方式。

基础命令是: hide empty members. 这个命令会隐藏空白的方法和属性。

empty members 外,你可以用:

  • empty fields 或者 empty attributes 空属性,
  • empty methods 空函数,
  • fieldsattributes 隐藏字段或属性,即使是被定义了
  • methods 隐藏方法,即使是被定义了
  • members 隐藏字段 和 方法,即使是被定义了
  • circle 类名前带圈的,
  • stereotype 原型。

同样可以使用 hideshow 关键词,对以下内容进行设置:

  • class 所有类,
  • interface 所有接口,
  • enum 所有枚举,
  • <<foo1>> 实现 foo1 的类,
  • 一个既定的类名。

你可以使用 show/hide 命令来定义相关规则和例外。

@startumlclass Dummy1 {
+myMethods()
}class Dummy2 {
+hiddenMethod()
}class Dummy3 <<Serializable>> {
String name
}hide members
hide <<Serializable>> circle
show Dummy1 methods
show <<Serializable>> fields@enduml
UML与软件建模:第二次作业(类图中类的表示)
(12)隐藏类
你也可以使用 show/hide 命令来隐藏类

如果你定义了一个大的!included 文件,且想在文件包含之后隐藏部分类,该功能会很有帮助。

@startumlclass Foo1
class Foo2Foo2 *-- Foo1hide Foo2@enduml

UML与软件建模:第二次作业(类图中类的表示)

(13)泛型(generics)
你可以用 <> 来定义类的泛型。
@startumlclass Foo<? extends Element> {
int size()
}
Foo *- Element@enduml

UML与软件建模:第二次作业(类图中类的表示)

(14)指定标记(Spot)
通常标记字符 (C, I, E or A) 用于标记 类(classes), 接口(interface), 枚举(enum)和 抽象类(abstract classes).

但是当你想定义原型时,可以增加对应的单个字符及颜色,来定义自己的标记(spot),就像下面一样:

@startumlclass System << (S,#FF7700) Singleton >>
class Date << (D,orchid) >>
@enduml

UML与软件建模:第二次作业(类图中类的表示)

(15)包
你可以通过关键词 package 声明包,同时可选的来声明对应的背景色(通过使用html色彩代码或名称)。

注意:包可以被定义为嵌套。

@startumlpackage "Classic Collections" #DDDDDD {
Object <|-- ArrayList
}package net.sourceforge.plantuml {
Object <|-- Demo1
Demo1 *- Demo2
}@enduml

UML与软件建模:第二次作业(类图中类的表示)

(16)包样式
包可以定义不同的样式。

你可以通过以下的命令来设置默认样式 : 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

UML与软件建模:第二次作业(类图中类的表示)

你也可以参考下面的示例来定义包之间的连线:
@startumlskinparam packageStyle rectanglepackage foo1.foo2 {
}package foo1.foo2.foo3 {
class Object
}foo1.foo2 +-- foo1.foo2.foo3@enduml

UML与软件建模:第二次作业(类图中类的表示)

(17)命名空间(Namespaces)@startumlclass BaseClassnamespace 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

UML与软件建模:第二次作业(类图中类的表示)

(18)自动创建命名空间
使用命令 set namespaceSeparator ??? 你可以自定义命名空间分隔符(为 “.” 以外的字符).
@startumlset namespaceSeparator ::
class X1::X2::foo {
some info
}@enduml

UML与软件建模:第二次作业(类图中类的表示)


禁止自动创建包则可以使用 set namespaceSeparator none.

@startumlset namespaceSeparator none
class X1.X2.foo {
some info
}@enduml
(19)棒棒糖 接口
需要定义棒棒糖样式的接口时可以遵循以下语法:
  • bar ()- foo
  • bar ()-- foo
  • foo -() bar
@startuml
class foo
bar ()- foo
@enduml

UML与软件建模:第二次作业(类图中类的表示)

(20)改变箭头方向
类之间默认采用两个破折号 -- 显示出垂直 方向的线. 要得到水平方向的可以像这样使用单破折号 (或者点):
@startuml
Room o- Student
Room *-- Chair
@enduml

UML与软件建模:第二次作业(类图中类的表示)

你也可以通过改变倒置链接来改变方向
@startuml
Student -o Room
Chair --* Room
@enduml

UML与软件建模:第二次作业(类图中类的表示)

也可通过在箭头内部使用关键字, 例如left, right, up 或者 down,来改变方向
@startuml
foo -left-> dummyLeft
foo -right-> dummyRight
foo -up-> dummyUp
foo -down-> dummyDown
@enduml

UML与软件建模:第二次作业(类图中类的表示)

(21)关系类
你可以在定义了两个类之间的关系后定义一个 关系类 association class 例如:
@startuml
class Student {
Name
}
Student "0..*" - "1..*" Course
(Student, Course) .. Enrollmentclass Enrollment {
drop()
cancel()
}
@enduml

UML与软件建模:第二次作业(类图中类的表示)

也可以用另一种方式:
@startuml
class Student {
Name
}
Student "0..*" -- "1..*" Course
(Student, Course) . Enrollmentclass Enrollment {
drop()
cancel()
}
@enduml

UML与软件建模:第二次作业(类图中类的表示)

 

二、班级学生管理系统

属性:

(1)基本信息:学号:varcher  姓名:string  年龄:int  班级:string  班级职务:string

(2)课程信息:学号:varcher 姓名:string 课程名:string 教师:string  教室:string

(3)成绩信息:学号:varcher 姓名:string 成绩:string 课程名:string

@startuml class 学生基本信息{

学号:varchar

姓名:string

年龄:int

班级:string

班级职务:string

__

+修改信息()

+保存()

}

class 学生课程信息{

学号:varchar

姓名:string

课程名:string

教师:string

教室:string

__

+修改课程()

+保存()

+返回()

+查看课程()

+课程成绩()

}

class 学生成绩信息{

学号:varchar

姓名:string

成绩:string

课程名:string

__

+查看成绩()

+返回()

}

学生基本信息<–right–学生课程信息

学生基本信息<–学生成绩信息

@enduml

UML与软件建模:第二次作业(类图中类的表示)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900