3

我的模块中有以下代码:

Class Studentdatabase
{
public List <student> studentslist;
public void UpdateStudentDetailsinlist();
}


Class student
{
public string name;
public int age;
public int marks;
public student friend;
}

我的模块用数据填充这个数据库,这个数据库将被另一个模块使用。

studentslist [0]
name - Trevor
age -12
marks - 33
friend - 
    name - Sam
    age - 12
    marks - 45

studentslist [1]
name - Warren
age -13
marks - 63
friend - 
    name - Sam
    age - 12
    marks - 45

studentslist [2]
name - Sam
age -12
marks - 45
friend - null

我的要求是,如果学生列表 [2] (Sam) 的分数更新为 48,则必须自动更新学生列表 [0] 和学生列表 [1] 中的 Sam 的分数(也必须更新)。这如何在模块中实现?

4

5 回答 5

2

您可以如下使用。

class StudentContext
{
    public List<Student> Studentslist { get; set; }

    public void AddStudent(Student student)
    {
        if (null == Studentslist)
        {
            Studentslist = new List<Student>();
        }
        Studentslist.Add(student);
    }

    public void AddFriend(Student student,Student friendStudent)
    {
        Studentslist.Where(x => x.StudentId == student.StudentId).FirstOrDefault().Friend = friendStudent;
    }
}

class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public int Marks { get; set; }
    public Student Friend { get; set; }
}

public class Test
{
    public static void Main()
    {
        Student student1 = new Student();
        student1.StudentId = 1;
        student1.Name = "A";
        student1.Marks = 100;

        Student student2 = new Student();
        student2.StudentId = 2;
        student2.Name = "AB";
        student2.Marks = 10;

        StudentContext studentContext = new StudentContext();
        studentContext.AddStudent(student1);
        studentContext.AddStudent(student2);
        studentContext.AddFriend(student1, student2);

        student1.Marks = 50;
        student2.Marks = 77;
    }
}
于 2013-10-23T07:14:56.577 回答
2

好吧,首先您需要一个标识符(某种类型的 - Guid、int 等),这将是您的主键。

需要保存在数据库中的实际值是朋友的标识符。

Class student
{
   public int id;
   public string name;
   public int age;
   public int marks;
   public int StudentFriendId;
}
于 2013-10-23T06:25:26.870 回答
1

您应该遵循以下结构并将 id 而不是整个实例放在朋友中

Class Studentdatabase
{
  public List <student> studentslist;
  public void UpdateStudentDetailsinlist();
}


   Class student
   {
      public int id,
      public string name;
      public int age;
      public int marks;
      public int friend; // it will contain id of that friend in student
   }
于 2013-10-23T06:50:43.013 回答
1

您应该使用相同的参考,它将反映在所有地方。

例子:

Student sam = new Student();
sam.Marks = 45;
//... other sam properties


Student warren = new Student();
// initialize all waren properties
waren.friend = sam;

// do same for trevor
Student trevor = new Student();


List<Student> students = new List<Student>();
students.Add(sam);
students.Add(waren);
students.Add(trevor);

//Now waren/trevor holds same student reference which is in student list so any change in sam will be reflected in friend of waren/trevor
sam.Marks = 48;// reflected at all 3 places.

如果要更改行为,可以更改Student为 struct(从类)或将 sam 的副本/克隆分配给 waren/trevor 的朋友

于 2013-10-23T06:22:30.380 回答
0

首先,唯一的标识符是必须的,就像一个整数id字段。如果Name财产足够好,那可以,但这通常不是一个好主意(这意味着只有一个学生可以有一个特定的名字)。一些选项:

  1. 正如 Tilak 所说,更新相同的参考作品。

  2. 如果您不能确定这一点,一个想法是强制在整个应用程序中为每个唯一的学生 (id) 维护一个单一的参考。

    class Student
    {
        //ideally properties
        public int id
        public string name;
        public int age;
        public int marks;
        public Student friend; 
    
        static readonly Dictionary<int, Student> students = new Dictionary<int, Student>();
    
        //a static factory method with whatever definition, but int id is a must
        public static Student Create(int id, string name, int age, int marks)
        {
            Student student;
            return students.TryGetValue(id, out student) 
                 ? student 
                 : new Student(id, name, age, marks)     
        }
    
        //private constructor, with whatever definition.
        private Student(int id, string name, int age, int marks)
        {
    
        }
    }
    

    压倒一切EqualsGetHashCode赋予它更好的意义。

  3. 如果上面的内存太多并且更新直接发生在数据库中,您可以避免类friend上的属性/字段,Student但提供一种从 db 查询的方法。就像是:

    class Student
    {
        //ideally properties
        public int id
        public string name;
        public int age;
        public int marks;
    
        private int friendId; //private it is
    
        public Student GetFriend()
        {
            return db.GetStudent(friendId);
        }
    }
    
于 2013-10-23T07:36:13.947 回答