这是我第一个问题的编辑: 所以我再次检查了作业。 所有代码都在工作,因为我的教授希望它能够工作,但只有我之前提出的问题是我的问题。 - 如何直接从列表中执行方法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);
}
}
}
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);
}
}
}
两个Console.writeline的输出必须是:
list[1].ShowGrade(0)
99
list[2].ShowGrade(3)
答案 0 :(得分:1)
您有一个课程列表,其中的学生公开为s
:
Console.Writeline(list[1].s.ShowGrade(0));
因此,您需要访问s
的{{1}}字段。不过,我建议您使用属性而不是公共字段:
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
类之外使用它。< / p>
答案 1 :(得分:1)
list[n]
会返回一个Course
,其中有一个Student
属性(一个课程可以有一个学生?),您可以从中显示成绩。
您的Student
课程需要此方法:
public int ShowGrade(int index)
{
return gradesList[index];
}
然后你可以打电话:
list[n1].s.ShowGrade(n2);
答案 2 :(得分:0)
您的代码肯定需要修改,因为存在问题,即
new Course ("C#", s.s_name = "Bob", s.gradesList = new List<int>(){100, 99, 85})
将无法编译,似乎您对传递构造函数参数&amp; object initialization
我不能使用构造函数
您只能在某种程度上使用对象初始化,例如,给定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);
}
击> <击> 撞击>
刚才意识到你说你无法将ShowGrade
添加到Course
类,在这种情况下你只需要从Student
即<{1}}获取Course
属性/ p>
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));
答案 3 :(得分: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();
}
}