UML 类图

元素

可见性符号

符号 含义
+ public
- private
# protected
~ 默认

字段

类型 表示法 示例
普通字段 可见性 名称 : 类型 - id : int
属性 可见性 名称 : 类型 {get set} + Name : string {get set}
静态字段 使用下划线 - ++value : float++
常量 可见性 名称 : 类型 = 值 {readOnly} + PI : double = 3.14 {readOnly}

方法

静态方法:加下划线

抽象方法:用斜体字表示

构造函数:与类型相同,不加返回类型

cs 复制代码
    public abstract class Animal
    {
        //构造函数
        public Animal(string type)
        {


        }

        //普通方法
        public void SayHi(string type)
        {

            Console.WriteLine("动物叫");
        }

        //抽象方法
        public abstract void Eat();

        //静态方法
        public static bool Check(string type)
        {
            return true;
        }
    }

接口

加<<interface>>标识

抽象类

方法一:类名不是斜体,但是加<<abstract>>标识。

方法二:类名斜体。

关系

详情请看此篇文章 :UML 关系详解-CSDN博客

枚举

使用<<enumeration>>标注

案例

cs 复制代码
//动物类型
public enum AnimalType
{
    Carnivores,//食肉动物
    Herbivore,//食草动物
    Omnivore,//杂食动物
}

public abstract class Animal
{
    public AnimalType animalType;
    public string Name { get; set; }
    public static float value;
    //构造函数
    public Animal()
    {


    }

    //普通方法
    public void Speak(string type)
    {
        Console.WriteLine("动物叫");
    }

    //抽象方法
    public abstract void Eat();

    //静态方法
    public static bool Check(string type)
    {
        return true;
    }
}

//老虎
public class Tiger:Animal
{
    public Tiger() : base()
    {
        animalType = AnimalType.Carnivores;
        Name = "老虎";
    }

    public override void Eat()
    {
        Console.WriteLine("吃肉!");
    }
}


//羊
public class Sheep:Animal
{
    public Sheep() : base()
    {
        animalType = AnimalType.Herbivore;
        Name = "羊";
    }

    public override void Eat()
    {
        Console.WriteLine("吃草!");
    }
}

//狗
public class Dog:Animal
{
    public Dog() : base()
    {
        animalType = AnimalType.Omnivore;
        Name = "狗";
    }

    public override void Eat()
    {
        Console.WriteLine("啥都吃!");
    }
}

public class Keeper
{
    public string Name { get; private set; }
    public Keeper(string _name)
    {
        Name = _name;
    }
    
    public void GetAnimal(Animal animal)
    {
        Console.WriteLine($"动物是:{animal.Name}");
    }
    
    public void KeepAnimal(Animal animal)
    {
        animal.Eat();
    }
}
相关推荐
吴声子夜歌16 小时前
PlantUML——状态图
uml·plantuml·状态图
吴声子夜歌1 天前
PlantUML——序列图
uml·plantuml·序列图
吴声子夜歌1 天前
PlantUML——活动图
uml·plantuml·活动图
吴声子夜歌2 天前
PlantUML——类图(一)
uml
吴声子夜歌2 天前
PlantUML——类图(二)
uml·plantuml·类图
吴声子夜歌2 天前
PlantUML——对象图
uml·plantuml·对象图
吴声子夜歌3 天前
PlantUML——用例图
uml·plantuml
rolt5 天前
PlantUML描述《分析模式》第4章企业财务观察(1)
产品经理·架构师·uml·系统工程
KobeSacre6 天前
UML 学习
学习·uml
hssfscv8 天前
软件设计师2021上、下上午题错题解析+2022上、下下午题训练5道 练习真题训练16
笔记·设计模式·uml