使用枚举(构造列表)
您可能遇到过这样的情况,您声明了某个枚举,并且想要评估某个对象以找出它满足哪些枚举类别,即一个对象可能满足将其分类为多个枚举的条件。
一个例子是,假设在一所大学里,我有各种课程,并且其中有来自不同专业的学生的列表,参加相同的课程。 因此,我们有
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 .NET 中,枚举值集通常使用 Flags 枚举:
In .NET, sets of enum values are usually realized using a Flags enumeration:
Java有EnumSet。我不能代表 C#。
Java has EnumSet. I can't speak for C#.