如何在 C# 中实现 class - 学生关系?
How to implement the class - Student relationship in c#?
我想实现一个系统,它表示 ClassRoom-Student 关系。我想施加限制,每个 ClassRoom 可以有任意数量的学生,但一个学生只能在一个 ClassRoom。
我创建了两个 类 - ClassRoom 和 Student。我在 Class ClassRoom.How 中创建了一个列表,我是否确保没有人可以在两个教室中插入同一个学生。
在 C# 中给定一个 IEnumerable<ClassRoom> classRooms
,其中 ClassRoom
有一个 属性 Students
即 IEnumerable<Student>
public bool HasStudent(Student student)
{
return classRooms.Any(c => c.Students.Any(s => s == student));
}
希望是你的经验水平:
教室
public class ClassRoom
{
private List<Student> students = new List<Student>();
public bool Contains(Student student)
{
return this.students.Contains(student);
}
public void Add(Student student)
{
if (!Contains(student))
this.students.Add(student);
student.StudentClassRoom = this;
}
public void Remove(Student student)
{
// if this contains, remove it anyway...
if(Contains(student))
this.students.Remove(student);
// but do not set ClassRoom of student if she/he does not sit in this.
if (student.StudentClassRoom != this)
return;
student.StudentClassRoom = null;
}
}
学生
public class Student
{
private ClassRoom stdClsRoom;
public ClassRoom StudentClassRoom
{
get { return this.stdClsRoom; }
set
{
if (value == this.stdClsRoom) //from null to null; from same to same;
return; //do nothing
if (this.stdClsRoom != null) //if it set from something
{
ClassRoom original = this.stdClsRoom;
this.stdClsRoom = null; // set field to null to avoid Whosebug
original.Remove(this); // remove from original
}
this.stdClsRoom = value; // set field
if (value != null) //if it set to something
value.Add(this); // add to new
}
}
}
我想实现一个系统,它表示 ClassRoom-Student 关系。我想施加限制,每个 ClassRoom 可以有任意数量的学生,但一个学生只能在一个 ClassRoom。
我创建了两个 类 - ClassRoom 和 Student。我在 Class ClassRoom.How 中创建了一个列表,我是否确保没有人可以在两个教室中插入同一个学生。
在 C# 中给定一个 IEnumerable<ClassRoom> classRooms
,其中 ClassRoom
有一个 属性 Students
即 IEnumerable<Student>
public bool HasStudent(Student student)
{
return classRooms.Any(c => c.Students.Any(s => s == student));
}
希望是你的经验水平:
教室
public class ClassRoom
{
private List<Student> students = new List<Student>();
public bool Contains(Student student)
{
return this.students.Contains(student);
}
public void Add(Student student)
{
if (!Contains(student))
this.students.Add(student);
student.StudentClassRoom = this;
}
public void Remove(Student student)
{
// if this contains, remove it anyway...
if(Contains(student))
this.students.Remove(student);
// but do not set ClassRoom of student if she/he does not sit in this.
if (student.StudentClassRoom != this)
return;
student.StudentClassRoom = null;
}
}
学生
public class Student
{
private ClassRoom stdClsRoom;
public ClassRoom StudentClassRoom
{
get { return this.stdClsRoom; }
set
{
if (value == this.stdClsRoom) //from null to null; from same to same;
return; //do nothing
if (this.stdClsRoom != null) //if it set from something
{
ClassRoom original = this.stdClsRoom;
this.stdClsRoom = null; // set field to null to avoid Whosebug
original.Remove(this); // remove from original
}
this.stdClsRoom = value; // set field
if (value != null) //if it set to something
value.Add(this); // add to new
}
}
}