如何从main中访问嵌套类中的方法

本文关键字:方法 嵌套 访问 main | 更新日期: 2023-09-27 18:15:57

这是我的第一个问题的编辑:所以我又检查了一遍作业。所有的代码都像我的教授想要的那样工作,但只有我之前问的问题是我的问题。-我怎么能执行ShowGrade方法直接从列表(当然)像:list[1].ShowGrade(0)不使用list[1]. s.w ShowGrade(0) ??我把所有的代码都写在这里。Main中的两条控制台线是他想要的(以及他想要的方式),我对任务的限制是:1. 嵌套类学生类课程(一个学生一个课程)。2. 没有构造函数,只有默认构造函数。3.ShowGrade方法将不在Course类中。4. 没有操作员。(点)在ShowGrade,只有[]5. 方法Q1中只有一个查询。

所以我的代码是:

课程。cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HW_1
{
    public class Course 
    {
        private delegate int Del(Course c);
        internal string courseName { get; set; }
        public  class Student
        {
            internal string stuName { get; set; }
            internal List<int> gradesList { get; set; }
            //internal int ShowGrade(int index)
            //{
            //    return gradesList[index];
            //}
        }
        internal Student s = new Student();
        public override string ToString()
        {
            string gr = null;
            foreach (var g in s.gradesList)
                gr += g + " ";
            return string.Format("{0, -6} {1, -14} {2, -10}", courseName, s.stuName, gr);
        }
    }
}

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HW_1
{
    class Program
    {
        private delegate bool Del(Course c);
        static void Main(string[] args)
        {
            List<Course> list = new List<Course>
            { 
                new Course {courseName = "C#", s = new Course.Student {stuName = "Jojo", gradesList = new List<int>(){10, 20, 100}}},
                new Course {courseName = "C", s = new Course.Student {stuName = "Bambi", gradesList = new List<int>(){99}}},
                new Course {courseName = "Java", s = new Course.Student {stuName = "Bambi", gradesList = new List<int>(){}}}
            };
            Console.WriteLine("List of courses:");
            Print(list);
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("Press P / p for students who passed in average 60 and those who didn't.");
            Console.WriteLine("");
            Console.WriteLine("Press # for C# courses and others.");
            Console.WriteLine("");
            Console.WriteLine("Press any other key for courses with student who have at least one grade of 100 and all oter courses.");
            char ch = (char)Console.Read();
            Del d = ((ch == 'P' || ch == 'p') ? (Del)(c => c.s.gradesList.Count > 0 && c.s.gradesList.Average() >= 60) : ((ch == '#') ? (Del)(c => c.courseName == "C#") : (Del)(c => c.s.gradesList.Contains(100))));
            var x = Q1 <IGrouping<bool, Course>>(list, d);
            Print(x);
            Console.WriteLine("");
            //Console.WriteLine(list[1].ShowGrade(0));
            //Console.WriteLine(list[2].ShowGrade(3));
        }
        static IEnumerable<T> Q1<T>(IEnumerable<Course> list, Del d)
        {
            var query =
                from c in list
                orderby d(c)
                group c by d(c) into g
                select g;
            return (IEnumerable<T>)query;
        }
        static void Print(IEnumerable<IGrouping<bool, Course>> list)
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Q1 Results:");
            foreach (var g in list)
            {
                Console.WriteLine();
                Console.WriteLine(g.Key);
                Console.WriteLine("---------------");
                foreach (var c in g)
                    Console.WriteLine(c);
            }
        }
        static void Print<T>(IEnumerable<T> list)
        {
            foreach (var l in list)
                Console.WriteLine(l);
        }

    }
}

两个控制台的输出。Writeline必须是:

list[1].ShowGrade(0)
99
list[2].ShowGrade(3)

如何从main中访问嵌套类中的方法

您有一个课程列表,学生被暴露为s:

Console.Writeline(list[1].s.ShowGrade(0));

因此需要访问list[x]s字段。但是,我建议您使用属性而不是公共字段:

class Course
{
    internal string c_name {get; set;}
    public Student Student { get; private set; }
    public Course()
    {
        this.Student = new Student();
    }
}
我实际上建议您将Student创建为一个非内部类。内部类应该只在本地使用。
class Student
{
    internal s_name {get; set;}
    internal List<int> gradesList {get; set;}
}

如果你想保持Student作为一个内部类,你必须使其public ,以便在Course类之外使用它。

list[n]返回一个Course,它有一个Student属性(一个课程可以有一个学生?),您想从这里显示成绩。

你的Student类需要这个方法:

public int ShowGrade(int index)
{
   return gradesList[index];
}

那么你可以调用:

list[n1].s.ShowGrade(n2);

您的代码肯定需要修改,因为存在以下问题:

new Course ("C#", s.s_name = "Bob", s.gradesList = new List<int>(){100, 99, 85})

不会编译,似乎你正在混淆传递构造函数参数&对象的初始化。

我不能使用构造函数

你只能在一定程度上使用对象初始化,例如,c_name是公共的,你可以这样做:

var course = new Course()
{
    c_name = "C#"
}

但是,你不能这样做:

var course = new Course()
{
    s.gradeList = ...
}

你只需要手动设置这个

var course = new Course()
{
    c_name = "C#"
};
course.s.gradeList = new List<int>() { ... }

为了回答你的问题,我会在这里使用一点封装,并在你的Course类中暴露一个ShowGrade方法,它会给你想要的代码,即

Console.Writeline(list[1].ShowGrade(0));

在内部,Course只会将调用委托给学生实例,例如

public int ShowGrade(int grade)
{
    return s.ShowGrade(grade);
}

刚刚意识到你说你不能将ShowGrade添加到Course类,在这种情况下,你只需要从Course访问Student属性,即

 list[0].s.ShowGrade(0);

你需要的是这样的代码:

list[0].ShowGrade(0);

但是不能修改Course类——唯一的替代方法是使用扩展方法,例如

public static class CourseExt
{
    public static int ShowGrade(this Course course, int grade)
    {
        return course.s.ShowGrade(grade);
    }
}
...
Console.WriteLine(list[0].ShowGrade(0));

这是您要求的代码:

class Course
{
    internal string CourseName { get; set; }
    public Student s { get; set; }
    public class Student
    {
        internal string StudentName { get; set; }
        internal IEnumerable<int> GradesList { get; set; }
        public int ShowGrade(int index)
        {
            if (GradesList == null)
                throw new NullReferenceException();
            return GradesList.ElementAt<int>(index);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<Course> list = new List<Course>()
    {
        new Course () { CourseName = "C#", 
            s =  new Course.Student() { StudentName = "Bob", 
                                                                            GradesList = new List<int>() { 100, 99, 85 }}},  
        new Course () { CourseName = "Java", 
            s =  new Course.Student() { StudentName = "Bobi", 
                                                                            GradesList = new List<int>(){ 99, 90, 88 }}},
        new Course (){ CourseName = "C", 
            s = new Course.Student() { StudentName = "Roni", 
                                                                            GradesList = new List<int>()}},
        new Course (){ CourseName = "SQL", 
            s = new Course.Student() { StudentName = "Sean", 
                                                                            GradesList = new List<int>(){ 75, 62, 55 }}}
    };
        Console.WriteLine(list[0].s.ShowGrade(1));
        Console.ReadKey();
    }
}