再次编辑:我想我现在明白了。那么我需要做的就是使用我希望能够访问的类的当前类冒号?人:学生,或人:老师,对吗?
我目前正在尝试学习面向对象编程的来龙去脉。目前我有一个新对象,如下所示:
class student
{
int grade; //0 - 100 as opposed to the characters A, B, C, etc.
string teacher; //For the teacher of the class the student is in.
}
class teacher
{
int courseAverage;
string name;
//teacher.name would correspond to the teacher in the student class.
}
class Program
{
student Jim;
Jim = new student();
teacher John;
John = new teacher();
}
static void grades()
{
Jim.grade = 100;
}
static void teacher()
{
Jim.teacher = "John Smith";
}
static void average()
{
int average; //For the sake of simplicity I'll leave the average at an int.
average = (Jim.grade + Sue.grade + Sally.grade + Robert.grade) / 4;
/*A loop would be set before the average variable to
ensure that only students of John would be counted in this.*/
}
static void teacheraverage()
{
John.courseAverage = average;//from the average method.
}
编辑:
我想做的是修改另一个类的信息。但是,我想在程序类中以一种方法修改来自 Jim 学生的信息。一种计算具有给定教师的学生的平均成绩的方法。
此外,我在这些中使用静态的唯一原因是因为这是我设法跨方法访问变量的唯一方法。我尝试使用静态方法跨类使用方法,但没有成功。还有另一种方法可以做到这一点吗?
我想以多种方法使用 Jim 学生。一个决定吉姆的成绩,另一个决定老师。在这种情况下,我想使用不同的方法,以便我可以了解它是如何完成的。
好吧,看来我的理解不正确。我将尝试类方法中的方法。