[英]A property of the a class refers to the same class. How to manage the instance
我的模塊中有以下代碼:
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標記(反向)。 如何在模塊中實現?
好吧,首先你需要一個標識符(某種類型 - Guid,int等),這將是你的主鍵。
需要保存在數據庫中的實際值是朋友的標識符。
Class student
{
public int id;
public string name;
public int age;
public int marks;
public int StudentFriendId;
}
您可以使用如下。
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;
}
}
您應該使用相同的參考,它將反映在所有地方。
例:
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的copy / clone分配給waren / trevor的朋友
你應該遵循以下結構,並在朋友中放入id而不是整個instnce
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
}
首先,唯一的標識符是必須的,類似於整數id
字段。 如果Name
屬性足夠好,那就行了,但這通常不是一個好主意(這意味着只有一個學生可以有一個特定的名字)。 一些選擇:
正如Tilak所說,更新相同的參考資料。
如果您無法確定這一點,一個想法是強制在每個唯一學生(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) { } }
覆蓋Equals
和GetHashCode
賦予它更好的意義。
如果上面的內存太多並且更新直接發生在數據庫中,則可以避免Student
類上的屬性/字段friend
,但提供了一種從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); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.