资源
正文
“在软件工程中,统一建模语言(UML)中的类图是一种静态结构图,它通过显示系统的类、它们的属性、操作(或方法)以及对象之间的关系来描述系统的结构。”
类图是面向对象建模的主要构建块。它用于应用结构的一般概念建模,以及将模型转换为编程代码的详细建模。类图也可用于数据建模。类图中的类表示主要元素、应用中的交互以及要编程的类。
在 UML 类图中,两个类之间可能有以下几种关系:
classDiagram
classA --> classB : 关联
classC --o classD : 聚合
classE --* classF : 组合
classG --|> classH : 继承(泛化)
classI ..|> classJ : (接口)实现
classK ..> classL : 依赖
classM -- classN : Link (Solid)
classO .. classP : Link (Dashed)
Tip
接口继承为三角,聚合组合为菱形,依赖关联为箭头,弱虚强实指源头。
关联(Association)
---
title: 关联(Association)
---
classDiagram
class Person {
+ string Name
+ Car Car
}
class Car {
+ string Model
}
Car --> Person
C# Javascript Python
1 2 3 4 5 6 7 8 9 10 class Person { public string Name { get ; set ; } public Car Car { get ; set ; } }class Car { public string Model { get ; set ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Person { constructor (name ) { this .name = name; this .car = null ; } setCar (car ) { this .car = car; } }class Car { constructor (model ) { this .model = model; } }const car = new Car ("Toyota" );const person = new Person ("John" ); person.setCar (car);console .log (person.name + " owns a " + person.car .model );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Person : def __init__ (self, name ): self .name = name self .car = None def set_car (self, car ): self .car = carclass Car : def __init__ (self, model ): self .model = model car = Car("Toyota" ) person = Person("John" ) person.set_car(car)print (f"{person.name} owns a {person.car.model} " )
聚合(Aggregation)
---
title: 聚合(Aggregation)
---
classDiagram
class Department {
+ string Name
+ List <Employee> Employees
}
class Employee {
+ string Name
}
Employee --o Department
C# Javascript Python
1 2 3 4 5 6 7 8 9 10 class Department { public string Name { get ; set ; } public List<Employee> Employees { get ; set ; } = new List<Employee>(); }class Employee { public string Name { get ; set ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Department { constructor (name ) { this .name = name; this .employees = []; } addEmployee (employee ) { this .employees .push (employee); } }class Employee { constructor (name ) { this .name = name; } }const department = new Department ("Engineering" );const employee1 = new Employee ("Alice" );const employee2 = new Employee ("Bob" ); department.addEmployee (employee1); department.addEmployee (employee2);console .log (department.name + " department has " + department.employees .length + " employees." );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Department : def __init__ (self, name ): self .name = name self .employees = [] def add_employee (self, employee ): self .employees.append(employee)class Employee : def __init__ (self, name ): self .name = name department = Department("Engineering" ) employee1 = Employee("Alice" ) employee2 = Employee("Bob" ) department.add_employee(employee1) department.add_employee(employee2)print (f"{department.name} department has {len (department.employees)} employees." )
组合(Composition)
---
title: 组合(Composition)
---
classDiagram
class House {
+ string Address
+ Room Room
}
class Room {
+ string Type
}
Room --* House
C# Javascript Python
1 2 3 4 5 6 7 8 9 10 class House { public string Address { get ; set ; } public Room Room { get ; set ; } }class Room { public string Type { get ; set ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class House { constructor (address ) { this .address = address; this .room = new Room ("Living Room" ); } }class Room { constructor (type ) { this .type = type; } }const house = new House ("123 Main St" );console .log (house.address + " has a " + house.room .type );
1 2 3 4 5 6 7 8 9 10 11 class House : def __init__ (self, address ): self .address = address self .room = Room("Living Room" ) class Room : def __init__ (self, type ): self .type = type house = House("123 Main St" )print (f"{house.address} has a {house.room.type } " )
继承(Inheritance)
继承表示类之间的“是一个”关系,子类继承父类的属性和方法。
子类可以继承父类的实现,也可以重写父类的功能。
---
title: 继承(Inheritance)
---
classDiagram
class Animal {
+ string Name
+ Speak()
}
class Dog {
+ Bark()
}
Dog --|> Animal
C# Javascript Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Animal { public string Name { get ; set ; } public void Speak () { Console.WriteLine("Animal makes a sound." ); } }class Dog : Animal { public void Bark () { Console.WriteLine("Dog barks." ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Animal { constructor (name ) { this .name = name; } speak ( ) { console .log (this .name + " makes a sound." ); } }class Dog extends Animal { constructor (name ) { super (name); } bark ( ) { console .log (this .name + " barks." ); } }const dog = new Dog ("Buddy" ); dog.speak (); dog.bark ();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Animal : def __init__ (self, name ): self .name = name def speak (self ): print (f"{self.name} makes a sound." )class Dog (Animal ): def __init__ (self, name ): super ().__init__(name) def bark (self ): print (f"{self.name} barks." ) dog = Dog("Buddy" ) dog.speak() dog.bark()
实现(Interface Implementation)
---
title: 实现(Interface Implementation)
---
classDiagram
class IDriveable {
+ Drive()
}
class Car {
+ Drive()
}
Car ..|> IDriveable
C# Javascript Python
1 2 3 4 5 6 7 8 9 10 11 12 interface IDriveable { void Drive () ; }class Car : IDriveable { public void Drive () { Console.WriteLine("Car is driving." ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Driveable { drive ( ) { throw "This method must be implemented" ; } }class Car extends Driveable { constructor (model ) { super (); this .model = model; } drive ( ) { console .log (this .model + " is driving." ); } }const car = new Car ("Toyota" ); car.drive ();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 from abc import ABC, abstractmethodclass Driveable (ABC ): @abstractmethod def drive (self ): pass class Car (Driveable ): def __init__ (self, model ): self .model = model def drive (self ): print (f"{self.model} is driving." ) car = Car("Toyota" ) car.drive()
依赖(Dependency)
---
title: 依赖(Dependency)
---
classDiagram
class Car {
+ StartEngine()
}
class Engine {
+ Start()
}
Car ..> Engine
C# Javascript Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Car { public void StartEngine (Engine engine ) { engine.Start(); } }class Engine { public void Start () { Console.WriteLine("Engine starts." ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Engine { start ( ) { console .log ("Engine starts." ); } }class Car { startEngine (engine ) { engine.start (); } }const engine = new Engine ();const car = new Car (); car.startEngine (engine);
1 2 3 4 5 6 7 8 9 10 11 class Engine : def start (self ): print ("Engine starts." )class Car : def start_engine (self, engine ): engine.start() engine = Engine() car = Car() car.start_engine(engine)