使用枚举(构造列表)

发布于 2024-09-18 12:10:40 字数 1641 浏览 3 评论 0原文

您可能遇到过这样的情况,您声明了某个枚举,并且想要评估某个对象以找出它满足哪些枚举类别,即一个对象可能满足将其分类为多个枚举的条件。

一个例子是,假设在一所大学里,我有各种课程,并且其中有来自不同专业的学生的列表,参加相同的课程。 因此,我们有

public class Course
{
    int Id;
    IList<Electrical> ElectricalStudents;
    IList<Computer> ComputerStudents;
    IList<Bioengineering> BioengineeringStudents;
    IList<Mechanical> MechanicalStudents;
}

,我的枚举如下

public enum ParentDepartment
{
    Electrical,
    Mechanical,
    Biology
}

,对于我拥有的每个课程对象,我需要确定学生属于哪个家长部门,以便我可以适当地使用它们(以便我可以邮寄这些相应的部门)

你会如何处理这种情况?最好的方法是什么?

我能想到的方法是在 Course 对象上有一个静态函数,其定义如下

public static IList<ParentDepartment> GetParentDepartments(Course course)
{
    public parentDepartmentList=new List<ParentDepartment>();
    if(ElectricalStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Electrical);
    if(ComputerStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Electrical);
    if(MechanicalStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Mechanical);
    if(BioengineeringStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Biology);
}

private void Add(IList<ParentDepartment> parentDepartmentList,ParentDepartment         parentdept)
{
    if(!parentDepartmentList.Contains(parentdept))
            parentDepartmentList.Add(parentdept);
}

希望事情很清楚。请注意这是一个假设的模型,请避免给我提供诸如更改模型之类的解决方案。

任何对功能的改进也将受到极大的赞赏。我只是不喜欢这里的实施方式。有没有更精简、更好的方法来处理同样的问题?

任何答案都将不胜感激。

PS:很抱歉让这个 C# 变得具体,但我认为这个概念可以扩展到任何语言。谢谢

You may have run into situations wherein you declare a certain enum and you want to evaluate a certain object to find out which enum categories it satisfies i.e an object may satisfy the conditions for it to be classified into more than one enum.

An example would be, say in an university i have various Course and this has the list of students attending from different specialization attending the same.
Thus we have

public class Course
{
    int Id;
    IList<Electrical> ElectricalStudents;
    IList<Computer> ComputerStudents;
    IList<Bioengineering> BioengineeringStudents;
    IList<Mechanical> MechanicalStudents;
}

and my enum is as follows

public enum ParentDepartment
{
    Electrical,
    Mechanical,
    Biology
}

and for every course object I have I need to identify which parentdepartments the students fall into so that I can use them appropriately( so that i can mail those respective departments)

How would you handle such a case? What is the best way to go about the same.

The way i can think of is have a static function on Course object whose definition is something like this

public static IList<ParentDepartment> GetParentDepartments(Course course)
{
    public parentDepartmentList=new List<ParentDepartment>();
    if(ElectricalStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Electrical);
    if(ComputerStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Electrical);
    if(MechanicalStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Mechanical);
    if(BioengineeringStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Biology);
}

private void Add(IList<ParentDepartment> parentDepartmentList,ParentDepartment         parentdept)
{
    if(!parentDepartmentList.Contains(parentdept))
            parentDepartmentList.Add(parentdept);
}

Hope the things are clear. Please note this an hypothetical model and please avoid giving me solution like changing the model.

Any improvement to the functions is also greatly appreciated. I just dont like the way things have been impelemented here. Is there a leaner and better method to handle the same?

Any answer is greatly appreciated.

P.S: Sorry for making this C# specific but I think the concept can be extended over to any language. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

背叛残局 2024-09-25 12:10:40

在 .NET 中,枚举值集通常使用 Flags 枚举

[Flags]
public enum ParentDepartments
{
    None = 0x0,
    Electrical = 0x1,
    Mechanical = 0x2,
    Biology = 0x4,
}

public static ParentDepartments GetParentDepartments(Course course)
{
    var departments = ParentDepartments.None;

    if (course.ElectricalStudents.Any())
        departments |= ParentDepartments.Electrical;

    if (course.ComputerStudents.Any())
        departments |= ParentDepartments.Electrical;

    if (course.MechanicalStudents.Any())
        departments |= ParentDepartments.Mechanical;

    if (course.BioengineeringStudents.Any())
        departments |= ParentDepartments.Biology;

    return departments;
}

In .NET, sets of enum values are usually realized using a Flags enumeration:

[Flags]
public enum ParentDepartments
{
    None = 0x0,
    Electrical = 0x1,
    Mechanical = 0x2,
    Biology = 0x4,
}

public static ParentDepartments GetParentDepartments(Course course)
{
    var departments = ParentDepartments.None;

    if (course.ElectricalStudents.Any())
        departments |= ParentDepartments.Electrical;

    if (course.ComputerStudents.Any())
        departments |= ParentDepartments.Electrical;

    if (course.MechanicalStudents.Any())
        departments |= ParentDepartments.Mechanical;

    if (course.BioengineeringStudents.Any())
        departments |= ParentDepartments.Biology;

    return departments;
}
动听の歌 2024-09-25 12:10:40

Java有EnumSet。我不能代表 C#。

Java has EnumSet. I can't speak for C#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文