【问题标题】:UML mapping with c#使用 C# 进行 UML 映射
【发布时间】:2010-08-15 05:22:11
【问题描述】:

有两个类 Person 和 Employee

当它映射到 c# 代码时

public class Person
{
    private string Name;
}

public class Employee : Person
{
    private string Department;

    public string GetName()
    {
        return "Person Name";
    }
}

我的问题是我可以在哪里为这个私有属性编写 getter 和 setter。如果可以,可以在相同的 Person 和 Employee 类中编写它们吗?映射没有问题吗?因为方法也在同一个类中(GetName())或者我必须使用单独的类来编写getter和setter。我对这个类图与代码的映射感到困惑。任何人都可以为我解决这个问题吗?

【问题讨论】:

    标签: c# class-design uml class-diagram


    【解决方案1】:

    首先,我会向您推荐 properties 方法,而不是 getters / setters 方法。

    我的看法:

    public class Person {
    
        private string name;
    
        public string Name {
            get {
                return this.name;
            }
        }
    }
    
    public class Department {
    
        private int id;
        private string name;
    
        public int ID {
            get {
                return this.id;
            }
        }
    
        public string Name {
            get {
                return this.name;
            }
        }
    }
    
    public class Employee : Person {
    
        private Department department;
    
        public Department Department {
            get {
                return this.department;
            }
        }
    }
    

    Employee.Name 返回在 Person 类中声明的员工姓名。

    【讨论】:

    • Employee类中的Department可以这样做吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多