[英]how to access method in nested class from main - C#
this is editing of my first question: so i checked the assignment again. 这是我第一个问题的编辑:所以我再次检查了作业。 all code is working as my professor want it to work, but only my question that i asked before is my problem. 所有代码都在工作,因为我的教授希望它能够工作,但只有我之前提出的问题是我的问题。 - how can i execute the method ShowGrade directly from the list (which is course) like: list[1].ShowGrade(0) without using list[1].s.ShowGrade(0) ?? - 如何直接从列表中执行方法ShowGrade(当然是这样):list [1] .ShowGrade(0)而不使用list [1] .s.ShowGrade(0)?? i will put here all my code. 我会把我所有的代码放在这里。 the two console lines in the Main is what he want (and how he wants it), my constraints for the assignment were: 1. nested class Student in class Course (one student for course). Main中的两个控制台线是他想要的(以及他想要的),我对作业的约束是:1。嵌套类课程中的学生课程(当然是一个学生)。 2. no constructors at all but the default constructors. 2.除了默认构造函数之外,根本没有构造函数。 3. the ShowGrade method won't be in the Course class. 3. ShowGrade方法不在Course类中。 4. no operator . 4.没有操作员。 (dot) in ShowGrade, only [] 5. in method Q1 will be only one query. (点)在ShowGrade中,只有[] 5.方法Q1中只有一个查询。
so my code is: 所以我的代码是:
course. 课程。 cs: 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 : 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);
}
}
}
the output for the two Console.writeline has to be: 两个Console.writeline的输出必须是:
list[1].ShowGrade(0)
99
list[2].ShowGrade(3)
You have a list of courses with students that are exposed as s
: 你必须与学生的课程 ,公开为列表s
:
Console.Writeline(list[1].s.ShowGrade(0));
You therefore need to access the s
field of list[x]
. 因此,您需要访问list[x]
的s
字段。 However, I'd suggest you to use a property instead of the public field: 但是,我建议您使用属性而不是公共字段:
class Course
{
internal string c_name {get; set;}
public Student Student { get; private set; }
public Course()
{
this.Student = new Student();
}
}
I actually recommend you to create Student
as a non-inner class . 我实际上建议你创建Student
作为非内部类 。 Inner classes are supposed to be used locally only. 内部类应该仅在本地使用。
class Student
{
internal s_name {get; set;}
internal List<int> gradesList {get; set;}
}
If you want to keep Student
as an inner class, you have to make it public
in order to use it outside the Course
class. 如果您希望将Student
作为内部课程,则必须将其public
才能在Course
之外使用它。
list[n]
returns a Course
, which has a Student
property (one course can have one student?), from where you want to show the grade. list[n]
返回一个Course
,它有一个Student
属性(一个课程可以有一个学生?),你想从哪里显示成绩。
Your Student
class needs this method: 您的Student
类需要此方法:
public int ShowGrade(int index)
{
return gradesList[index];
}
Then you can call: 然后你可以打电话:
list[n1].s.ShowGrade(n2);
Your code definitely needs to be revised as there are issues ie 你的代码肯定需要修改,因为有问题,即
new Course ("C#", s.s_name = "Bob", s.gradesList = new List<int>(){100, 99, 85})
won't compile, it seems like you are getting confused with passing constructor parameters & object initialization . 不会编译,似乎你对传递构造函数参数和对象初始化感到困惑。
I can't use constructors 我不能使用构造函数
You can only use object initialization to some extent, for example, given c_name
is public you could do: 您只能在某种程度上使用对象初始化,例如,给定c_name
是公共的,您可以这样做:
var course = new Course()
{
c_name = "C#"
}
However, you can't do: 但是,你做不到:
var course = new Course()
{
s.gradeList = ...
}
You just need to manually set this 你只需要手动设置它
var course = new Course()
{
c_name = "C#"
};
course.s.gradeList = new List<int>() { ... }
To answer your question though, I would use a bit of encapsulation here and expose a
ShowGrade
method in your
Course
class which would give you the code you want ie
要回答你的问题,我会在这里使用一些封装并在你的
Course
类中公开一个
ShowGrade
方法,它会给你你想要的代码,即
Console.Writeline(list[1].ShowGrade(0));
Internally,
Course
would just delegate the call onto the student instance eg
在内部,
Course
只会将调用委托给学生实例,例如
public int ShowGrade(int grade) { return s.ShowGrade(grade); }
Just realised you said you can't add ShowGrade
to the Course
class, in that case you just need to access the Student
property from the Course
ie 刚刚意识到你说你不能将ShowGrade
添加到Course
类中,在这种情况下你只需要从Course
访问Student
属性即
list[0].s.ShowGrade(0);
The penny just dropped on this one, you need to have the code like: 便士只是放在这一个,你需要有如下代码:
public static class CourseExt
{
public static int ShowGrade(this Course course, int grade)
{
return course.s.ShowGrade(grade);
}
}
...
Console.WriteLine(list[0].ShowGrade(0));
but can't modify the Course
class - the only alternative is to use an extension method eg 但不能修改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));
Here is the code you asked for: 以下是您要求的代码:
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.