Mermaid语法说明

类图(Class Diagram)

“In software engineering, a class diagram in the Unified Modeling Language (UML) is a type of static structure diagram that describes the structure of a system by showing the system’s classes, their attributes, operations (or methods), and the relationships among objects.”
-Wikipedia

class diagram一般用来表明应用(Application)的整体概念模型(general conceptual modeling),开发人员可根据class diagram使用具体的编程语言实现应用。

Note

Title以及Note在CSDN的渲染中暂时还不支持。

语法(Syntax)

\quad\quad class diagram中,类由三部分组成,即 类名属性以及方法

如何定义一个类

\quad\quad class diagram中,有两种方式可以定义一个类,直接给出例子

classDiagram
	class Student {
		-String name
		-int id
		+getName() String
		+getId() int
	}

	class Teacher 
	Teacher : -String name
	Teacher : -int id
	Teacher : +getName() String
	Teacher : +getId() int
Student
-String name
-int id
+getName() : String
+getId() : int
Teacher
-String name
-int id
+getName() : String
+getId() : int

\quad\quad 是的,正如你所见,mermaid支持函数的返回值类型后缀。其中第一种方法和我们定义类的过程比较类似。

可见性

  • + Public可见性
  • - Private可见性
  • # Protected可见性
  • ~ Package可见性

如何表示类之间的继承关系

classDiagram
	class Person {
		-String name
		+getName() String
	}
	
	class Eatable {
		<<interface>>
		+eat() void*
	}
	
	Person <|-- Student
	Eatable <|.. Student
	class Student {
		-String name
		+getName() String
		+eat() void
	}
Person
-String name
+getName() : String
«interface»
Eatable
+eat() : void
Student
-String name
+getName() : String
+eat() : void

类之间有哪些关系

符号描述
<|--Inheritance
<|..Realization
<--Association
<..Dependency
*--Composition
o--Aggregation
--Link(Solid)
..Link(Dashed)
  • 依赖关系(Dependency)
    \quad\quad 假设有两个类,类A与类B。类A的某个成员方法使用了类B,则说明类A依赖类B。是一种use-a的关系。
  • 关联关系(Association)
    \quad\quad 假设有两个类,类A与类B。类B做为类A的成员变量存在,类A也可做为类B的成员变量存在。
  • 聚合关系(Aggregation)
  • 组合关系(Composition)

\quad\quad 其中依赖关系是一种临时的关系,依赖关系主要体现在方法参数上,只有调用方法时才有关系。关联关系是一种长期的关系,主要体现在成员变量上,无论是否调用方法,这种关系都是存在的。

\quad\quad 从关联到聚合再到组合,关系逐步递进。关联关系双方是平级的,而聚合关系是一种整体与部分的关系。聚合关系中的部分离开整体仍可存在,组合关系中的部分离开整体则没有意义。

聚合是一种特殊的关联关系,它是较强的一种关联关系,强调的是整体与部分之间的关系,从语法上是无法区分的,只能从语义上区分,组合也是关联关系的一种特例,这种关系比聚合关系更强。它强调了整体与部分生命周期是一致的,而聚合的整体和部分之间在生命周期上没有什么必然关系。——《关联、聚合、组合的区别》

如何表示泛型

Square<Shape>
int id
List<int> position
-List<string> messages
setPoints(List<int> points)
getPoints() : List<int>
+setMessages(List<string> messages)
+getMessages() : List<string>
+getDistanceMatrix()

结尾

\quad\quad 最后,以mermaid网站上的示例程序结尾吧:

---
title: Animal example
---
classDiagram
    Animal <|-- Duck
    Animal <|-- Fish
    Animal <|-- Zebra
    Animal : +int age
    Animal : +String gender
    Animal: +isMammal()
    Animal: +mate()
    class Duck{
        +String beakColor
        +swim()
        +quack()
    }
    class Fish{
        -int sizeInFeet
        -canEat()
    }
    class Zebra{
        +bool is_wild
        +run()
    }
Animal
+int age
+String gender
+isMammal()
+mate()
Duck
+String beakColor
+swim()
+quack()
Fish
-int sizeInFeet
-canEat()
Zebra
+bool is_wild
+run()
  • 38
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值