本文仅作学习笔记与交流,不作任何商业用途,作者能力有限,如有不足还请斧正
本系列旨在通过补全学习之后,给出任意类图都能实现并做到逻辑上严丝合缝
学会这套规则,并看完面向对象补全计划文章之后,可以尝试实现任意逻辑的类图了
前置文章:
C# & Unity 面向对象补全计划 之 访问修饰符-CSDN博客
C# & Unity 面向对象补全计划 之 类class-CSDN博客
C# & Unity 面向对象补全计划 之 继承(字段与属性)-CSDN博客
1.类图的规则
我使用的类图软件:

类图规则介绍:
UML访问修饰符

UML属性(对应c#的字段与属性两部分)

举例:

UML方法的参数和返回值

举例:


2.举栗子
2.1类
比如我有一个Computer类,有一系列常见的属性与方法,我就可以画出类图

2.2继承
众所周知,电脑按照形态分为很多种,比如:
Desktop Computer,Laptop/Notebook,All-in-One PC

那么就可以画出子类对父类的继承(只举一个栗子):

代码实现:
cs
public class Computer
{
protected string cpu;
protected string gpu;
protected int rom;
protected int storage;
protected void UpdateOS()
{
// 更新操作系统逻辑
}
protected void CheckHardwareStatus()
{
// 检查硬件状态逻辑
}
protected void InstallSoftware(string software)
{
// 安装软件逻辑
}
protected void UninstallSoftware(string software)
{
// 卸载软件逻辑
}
protected void ConnectToNetwork()
{
// 连接网络逻辑
}
}
public class DesktopComputer : Computer
{
private string CaseType;
private int MonitorSize;
private void ReplaceHardware()
{
// 更换硬件逻辑
}
private void AdjustMonitorSettings()
{
// 调整显示器设置逻辑
}
}