0

这是我的第一个问题的编辑:所以我再次检查了作业。所有代码都按我的教授希望的那样工作,但只有我之前问过的问题才是我的问题。- 我如何直接从列表(当然)执行 ShowGrade 方法,例如:list[1].ShowGrade(0) 而不使用 list[1].s.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);
        }

    }
}

程序.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);
        }



    }
}

两个 Console.writeline 的输出必须是:

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

4 回答 4

1

您有一个包含以下学生的课程s列表:

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

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

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类外使用它。

于 2013-05-30T10:37:41.650 回答
1

list[n]返回 a Course,它有一个Student属性(一门课程可以有一个学生?),从您要显示成绩的位置。

你的Student班级需要这个方法:

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

然后你可以调用:

list[n1].s.ShowGrade(n2);
于 2013-05-30T10:38:08.077 回答
0

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

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>() { ... }

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

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

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

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

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

 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));
于 2013-05-30T10:47:37.330 回答
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();
    }
}
于 2013-05-30T10:56:08.730 回答