开发者

Polymorphism - exercise

Alright, so this is the exercise:

Define a class named student, containing three grades of students. The class will have a function that calculates the grades average. Now, define a class named student1 which will be derived from student and will add a function to calculate the sum of the grades.In the Main program, define a student variable and object of type student1. Perform placement of the object to variable and run the function of student1.

Note: This is not homework, I'm learning this my own. This is the code:

class Student
{
    protected int grade1, grade2, grade3;
    public Student(int grade1, int grade2, int grade3)
    {
        this.grade1 = grade1;
        this.grade2 = grade2;
        this.grade3 = grade3;
    }
    public double Average()
    {
        return (grade1 + 开发者_如何学Cgrade2 + grade3) / 3;
    }
}
class Student1 : Student
{
    public Student1(int grade1, int grade2, int grade3)
        : base(grade1, grade2, grade3)
    {
    }
    public double Sum()
    {
        return grade1 + grade2 + grade3;
    }
}
class Program
{
    static void Main(string[] args)
    {
    }
}

I don't really know what to do in the main class, how do I perform this placement and also, I wanted to know what's the benefit of doing it, let me know if I have mistakes so far, thanks alot.


OK: I guess this is what they're looking for, although the english is a little ropey:

1) Declare student variable

 Student s;

2) Declare Student1 object

 Student1 s1 = new Student1(1,2,3);

3) Perform placement of object to variable:

s = s1;

4) Run the function (Note you'll have to cast s to Student1 type to access the Type specific function Sum)

Console.WriteLine(((Student1)s).Sum());


Maybe this is what it means.. although its really badly worded in my eyes.

In the Main program, define a student variable and object of type student1. Perform placement of the object to variable and run the function of student1.

static void Main(string[] args)
{
    //define a student variable and object of type student1.
    Student student = new Student1(100, 99, 98); 

    //Perform placement of the object to variable and run the function of student1
    var sum = ((Student1)student ).Sum();

}


The primary flaw I see in your code from an OOP perspective is making Student1 extend from Student. When using inheritance, make sure it's a true extension (is a). You would be better served by making 1 student class and implementing the Sum and Average methods.

I think the following would be sufficient from an OOP perspective.

class Student
{
    protected int grade1, grade2, grade3;

    public Student(int grade1, int grade2, int grade3)
    {
       this.grade1 = grade1;
       this.grade2 = grade2;
       this.grade3 = grade3;
    }

    public double Average()
    {
        return (grade1 + grade2 + grade3) / 3;
    }

    public double Sum()
    {
        return grade1 + grade2 + grade3;
    }
}


Following exactly what was described by the exercise:

// Define a student variable.
Student s;

// And object of type Student1.
Student1 s1 = new Student1(10, 5, 8);

// Perform placement of the object to variable.
s = s1;

// And run the function of Student1.
// But it makes no sense...
s1.Sum();

// Maybe the exercise wants it:
((Student1)s).Sum();

// Which makes no sense too, since is making an idiot cast.
// But for learning purposes, ok.

Well, I don't believe this exercise is taking any advantage of polymorphism in C#. I suggest you reading the link to see a real usage sample.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜