我想使用对象列表
创建层次结构树树是这样的 我无法创建动态列表
//code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
class Student
{
string studentname;
class Department
{
string departmentname;
class Subject
{
string subjectname;
}
List<Subject> sub = new List<Subject>();
}
List<Department> dep = new List<Department>();
public void getdata()
{
List<Student> stu = new List<Student>();
}
}
}
答案 0 :(得分:0)
您可以从:
开始class Student
{
string studentname;
public Student(string studentname) {
this.studentname = studentname;
}
public List<Department> Departments = new List<Department>();
}
class Department
{
string departmentname;
public Department(string departmentname) {
this.departmentname = departmentname;
}
public List<Subject> Subjects = new List<Subject>();
}
class Subject
{
string subjectname;
public Subject(string subjectname) {
this.subjectname = subjectname;
}
}
以这种方式使用上述结构:
Student s = new Student("Student 1");
Department d = new Department("Department A");
s.Departments.Add(d);
d.Subjects.Add(new Subject("Subject A"));