Student Find(int id) { foreach (Student s in Class.Students) { if (s.id == id) return s; } return null; }
其实你可以使用 List<T> 或者 ArrayList。用 Add() 方法就可以添加。 你可以在 Get 中伪装成它是一个数组:public class ClassInfo { private List<Student> m_students;
public void InsertStudent(Student s) { m_students.Add(s); } public Student[] Students { return m_students.ToArray(); } public int ClassID { get; set; } public string ClassName { get; set; } ...}
public class ClassInfo { private List<Student> m_students;
public void InsertStudent(Student s) { m_students.Add(s); } public Student[] Students { get { return m_students.ToArray(); } } public int ClassID { get; set; } public string ClassName { get; set; } ...}
class Student //学生类 { private string no; private string name; private double chines; private double math; private double english; public Student(string no, string name, double chines, double math, double english) { this.no = no; this.name = name; this.chines = chines; this.math = math; this.english = english; } } class StudentList //班级类 { public Student[] students= new Student[34]; private static int count = 0;
public void setInfor(String sno,String sname,double schines,double smath,double senglish) { students[count] = new Student(sno, sname, schines, smath, senglish); count++; } public int Count() { return count - 1; } } 麻烦请问一下我这样写怎么在后面建立班级类调用Student[]不行!!!
{
foreach (Student s in Class.Students)
{
if (s.id == id) return s;
}
return null;
}
你可以在 Get 中伪装成它是一个数组:public class ClassInfo
{
private List<Student> m_students;
public void InsertStudent(Student s)
{
m_students.Add(s);
} public Student[] Students
{
return m_students.ToArray();
} public int ClassID { get; set; } public string ClassName { get; set; } ...}
{
private List<Student> m_students;
public void InsertStudent(Student s)
{
m_students.Add(s);
} public Student[] Students
{
get
{
return m_students.ToArray();
}
} public int ClassID { get; set; } public string ClassName { get; set; } ...}
class Student //学生类
{
private string no;
private string name;
private double chines;
private double math;
private double english;
public Student(string no, string name, double chines, double math, double english)
{
this.no = no;
this.name = name;
this.chines = chines;
this.math = math;
this.english = english;
}
}
class StudentList //班级类
{
public Student[] students= new Student[34];
private static int count = 0;
public void setInfor(String sno,String sname,double schines,double smath,double senglish)
{
students[count] = new Student(sno, sname, schines, smath, senglish);
count++;
}
public int Count()
{
return count - 1;
}
}
麻烦请问一下我这样写怎么在后面建立班级类调用Student[]不行!!!