a类的属性指的是同一个类。如何管理实例

时间:2013-10-23 06:20:46

标签: c# class reference

我的模块中有以下代码:

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标记(反向)。 如何在模块中实现?

5 个答案:

答案 0 :(得分:2)

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

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

Class student
{
   public int id;
   public string name;
   public int age;
   public int marks;
   public int StudentFriendId;
}

答案 1 :(得分: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;
    }
}

答案 2 :(得分: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的朋友

答案 3 :(得分: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
   }

答案 4 :(得分: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);
        }
    }