哦,是的,它的大脑时间很大。
这里是一些情况:我目前正忙于为学校作业进行简单的学生管理。我目前正在添加将学生添加到班级的选项。我为此使用的代码是这样的:
class Student
{
//alle properties van de student
public string studentnummer { get; set; } //student number
public string naam { get; set; } //name of the student
public string postcode { get; set; } //adres of the student
public string telefoonnummer { get; set; } //phone number og the student
//an constructor
public Student(string studentnummer1, string naam1, string postcode1, string telefoonnummer1)
{
studentnummer = studentnummer1;
naam = naam1;
postcode = postcode1;
telefoonnummer = telefoonnummer1;
}
//To string override in case you might want to know wether its part of my code
public override string ToString()
{
return $"Studentnummer: {studentnummer}, naam: {naam}, postcode: {postcode}, telefoonnummer :{telefoonnummer}";
//"aantal vakken: { String.Join(", ", vakken)}"
}
Run Code Online (Sandbox Code Playgroud)
这代表学生的个人信息。我在以下课程中创建了一个学生列表:
class Klas
{
public string Klascode { get; set; }
public string Klasnaam { get; set; }
public List<Student> Studentenlijst { get; set; } //list with students
public Klas(string klasnummer, string klasnaam, List<Student> studenten)
{
Klascode = klasnummer;
Klasnaam = klasnaam;
Studentenlijst = studenten;
}
static void Voegstudenttoe()
{
//this is where the students should be added to a klas inside the klassenlist
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我为可以放入学生的克拉森奠定了基础,但是我仍然想初始化那些克拉森:
class Program
{
static List<Klas> Klassenlijst = new List<Klas>();
public static void alleklassen()
{
//list with klassen and students
Klassenlijst.Add(new Klas("KL0001", "AO1-A", new List<Student>()
{
}
));
Klassenlijst.Add(new Klas("KL0002", "AO1-B", new List<Student>()
{
//this is where I used to create the students on the spot
}
));
Klassenlijst.Add(new Klas("KL0003", "AO2-A", new List<Student>()
{
}
));
Klassenlijst.Add(new Klas("KL0004", "GD3-C", new List<Student>()
{
}
));
Klassenlijst.Add(new Klas("KL0176", "STM4-P", new List<Student>()
{
}
));
}
static void Main(string[] args)
{
}
}
Run Code Online (Sandbox Code Playgroud)
这堂课提出了几个需要学生加入的知识(或者不需要,因为学生不一定要学习知识)。问题是我不能简单地在使用这些klassen的学生中添加学生,new student("studentnummer", "naam", "all the other variabels you can see inside of the Student class")而必须使用单独的function(Voegstudenttoe())来添加他们。
而且,如果您认为已经很多,那么在我可以将他们添加到列表之前,这些学生必须已经存在,因为某些学生可能并不熟悉,只能自己上学。
这一切最终导致我对如何做一无所知,因为我的小脑无法理解这样的事情。换句话说:我需要帮助。
那方法
static void Voegstudenttoe()
{
//this is where the students should be added to a klas inside the klassenlist
}
Run Code Online (Sandbox Code Playgroud)
不应该是静态的,因此您可以将学生添加到该Klas的特定实例。
接下来,您应该将“要添加的学生”作为参数传递。然后,您可以轻松地将其添加到列表中:
public void Voegstudenttoe(Student newStudent)
{
if (newStudent != null)
this.Studentenlijst.Add(newStudent);
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,Studentenlijst属性也可以是:
public List<Student> Studentenlijst { get; } = new List<Student>();
Run Code Online (Sandbox Code Playgroud)
这样一来,没有人可以取代列表,你一定有是一个列表(NOT NULL)。
(在注释之后添加)非空保证当然假设您不替换构造函数中的列表(可能为空值)。要么删除该参数,要么将所提供列表的每个学生(请进行空检查)添加到现有内部列表中。