C#编程新手求助:如何在Main方法中访问学生选课列表中的对象?
问题分析与解决方案
嘿,作为编程新手,你遇到的问题其实很典型——主要是类成员的访问权限、数据初始化逻辑,还有一些小语法问题在捣乱。我一步步帮你理清并修正:
1. 核心问题:无法访问类内集合的原因
你定义的studentList、courseList这些集合,在C#里默认是**私有(private)**的类成员,外部的Main方法根本看不到它们。解决这个问题有两种简单方式:
方式一:直接把字段设为公开(新手友好)
修改Admin类里的集合定义,加上public修饰符,让外部能直接访问:
public Dictionary<int, Student> studentList = new Dictionary<int, Student>(); public Dictionary<int, Professor> professorList = new Dictionary<int, Professor>(); public List<Course> courseList = new List<Course>(); public Dictionary<int, Student> studentCourses = new Dictionary<int, Student>();
方式二:用属性封装(更规范)
如果不想让外部随意修改集合内容,可以用只读属性封装,安全性更高:
public Dictionary<int, Student> StudentList { get; private set; } = new Dictionary<int, Student>(); public List<Course> CourseList { get; private set; } = new List<Course>(); // 其他集合同理
2. 修正数据初始化逻辑
你现在的StudentList()、CourseList()方法只是返回集合,但如果没调用这些方法,集合永远是空的!而且每次调用方法都会重新创建学生数据,最好把初始化逻辑放到Admin的构造函数里——创建Admin对象时会自动执行初始化:
class Admin { // 先定义公开的集合字段/属性 public Dictionary<int, Student> studentList = new Dictionary<int, Student>(); public Dictionary<int, Professor> professorList = new Dictionary<int, Professor>(); public List<Course> courseList = new List<Course>(); public Dictionary<int, Student> studentCourses = new Dictionary<int, Student>(); // 构造函数:创建Admin对象时自动跑这里的代码 public Admin() { LoadStudents(); LoadProfessors(); LoadCourses(); AssignStudentCourses(); } // 把原来的方法改成私有初始化方法 private void LoadStudents() { var Student1 = new Student() { FirstName = "Tom", LastName = " Jones", Password = "Test", EnrollmentId = 111111}; var Student2 = new Student() { FirstName = "Mark", LastName = " Thomas", Password = "Grace", EnrollmentId = 101203 }; var Student3 = new Student() { FirstName = "Jake", LastName = " Riley", Password = "SavedbyChrist1", EnrollmentId = 568903 }; var Student4 = new Student() { FirstName = "Olivia", LastName = " Beckam", Password = "GodisGood", EnrollmentId = 951357 }; var Student5 = new Student() { FirstName = "Myrai", LastName = " Bailey", Password = "ChildofGod", EnrollmentId = 741369 }; var Student6 = new Student() { FirstName = "Majea", LastName = " Bailey", Password = "CoastLand", EnrollmentId = 852146 }; studentList.Add(111111, Student1); studentList.Add(101203, Student2); studentList.Add(568903, Student3); studentList.Add(951357, Student4); studentList.Add(741369, Student5); studentList.Add(852146, Student6); } private void LoadProfessors() { // 先删掉代码里的`enter code here`占位符,不然编译会报错 professorList.Add(654321, new Professor { FirstName = "Grace", LastName = "Riley", Password = "Gracey1983", EnrollmentId = 654321 }); professorList.Add(852963, new Professor { FirstName = "Liam", LastName = "Beckam", Password = "Password", EnrollmentId = 852963 }); professorList.Add(359861, new Professor { FirstName = "Martha", LastName = "Hart", Password = "QwertYy00", EnrollmentId = 359861 }); professorList.Add(612832, new Professor { FirstName = "George", LastName = "Washington", Password = "Kid$nExtD00r", EnrollmentId = 612832 }); } private void LoadCourses() { var Accounts = new Course() { CourseID = 101, CourseName = " Accounts " }; var Bio_Psychology = new Course() { CourseID = 102, CourseName = " Bio_Psychology " }; var Business = new Course() { CourseID = 103, CourseName = " Business " }; var Chemistry = new Course() { CourseID = 104, CourseName = " Chemistry " }; var CSWDCA = new Course() { CourseID = 105, CourseName = " Computer Science/Web Design/Cybersecurity Accounts" }; var Psychology = new Course() { CourseID = 106, CourseName = " Psychology " }; var Statistics = new Course() { CourseID = 107, CourseName = " Statistics " }; var Advance_Math = new Course() { CourseID = 108, CourseName = "Advance Math" }; courseList.Add(Accounts); courseList.Add(Bio_Psychology); courseList.Add(Business); courseList.Add(Chemistry); courseList.Add(CSWDCA); courseList.Add(Psychology); courseList.Add(Statistics); courseList.Add(Advance_Math); } private void AssignStudentCourses() { var Student1 = studentList[111111]; if(Student1.Courses == null) { Student1.Courses = new List<Course>(); // 这里可以去掉?.,因为已经判断过Courses不为null了 Student1.Courses.Add(courseList[0]); Student1.Courses.Add(courseList[2]); Student1.Courses.Add(courseList[4]); } var Student2 = studentList[101203]; if(Student2.Courses == null) { Student2.Courses = new List<Course>(); Student2.Courses.Add(courseList[1]); Student2.Courses.Add(courseList[4]); Student2.Courses.Add(courseList[5]); Student2.Courses.Add(courseList[6]); Student2.Courses.Add(courseList[7]); } // 其他学生的选课逻辑同理,这里省略... studentCourses.Add(111111,Student1); studentCourses.Add(101203,Student2); // 其他学生添加同理... } }
3. 在Main方法中获取学生选课信息
现在你可以在Main里轻松通过Student ID提取信息了:
class Program { static void Main(string[] args) { // 创建Admin对象,自动完成所有数据初始化 Admin collegeAdmin = new Admin(); // 要查询的学生ID int targetId = 111111; // 先判断字典里有没有这个ID,避免报错 if(collegeAdmin.studentList.ContainsKey(targetId)) { Student student = collegeAdmin.studentList[targetId]; Console.WriteLine($"学生姓名:{student.FirstName} {student.LastName}"); Console.WriteLine("所选课程:"); foreach(var course in student.Courses) { Console.WriteLine($"- {course.CourseName} (课程ID: {course.CourseID})"); } } else { Console.WriteLine($"找不到ID为{targetId}的学生"); } } }
额外小提示
- 确保你的
Student类已经定义了Courses属性,不然会报错:
class Student { public string FirstName { get; set; } public string LastName { get; set; } public string Password { get; set; } public int EnrollmentId { get; set; } public List<Course> Courses { get; set; } // 这个必须要有! }
- 不要在代码里留
enter code here这种占位符,会导致编译失败 - 访问字典元素时,先用
ContainsKey()判断存在性,避免抛出异常
内容的提问来源于stack exchange,提问作者Miguél Alexander
相关产品推荐
相关产品推荐

