Pratiquer le Polymorphism
Le programme ci-dessous contient une superclasse appelée Shape
et trois classes dérivées appelées Rectangle
, Square
et Circle
. Le problème avec le code est que les méthodes ne sont pas correctement redéfinies puisque la sortie affiche 0.0
pour les trois instructions Console.WriteLine
, ce qui implique que les méthodes de la classe de base sont exécutées au lieu des méthodes de la classe dérivée.
Modifiez le code de manière à ce que les deux méthodes, à savoir getArea
et calculatePerimeter
, soient correctement redéfinies.
index.cs
999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Collections.Generic;
class Shape
{
// Perimeter is the length of the 'outline' of a shape
public float perimeter;
// Change code below this line
public float getArea()
{
return 0.0f;
}
protected float calculatePerimeter()
{
return 0.0f;
}
// Change code above this line
}
class Rectangle : Shape
{
float width;
float height;
public Rectangle(float width, float height)
{
this.width = width;
this.height = height;
this.perimeter = this.calculatePerimeter();
}
// Change code below this line
public float getArea()
{
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107using System; using System.Collections.Generic; class Shape { // Perimeter is the length of the 'outline' of a shape public float perimeter; // Change code below this line public float getArea() { return 0.0f; } protected float calculatePerimeter() { return 0.0f; } // Change code above this line } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = this.calculatePerimeter(); } // Change code below this line public float getArea() { return width * height; } protected float calculatePerimeter() { return width * 2 + height * 2; } // Change code above this line } class Square : Shape { float length; public Square(float length) { this.length = length * length; this.perimeter = calculatePerimeter(); } // Change code below this line public float getArea() { return length * length; } protected float calculatePerimeter() { return 4 * length; } // Change code above this line } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; this.perimeter = calculatePerimeter(); } // Change code below this line public float getArea() { return 3.14f * radius * radius; } protected float calculatePerimeter() { return 2.00f * 3.14f * radius; } // Change code above this line } class ConsoleApp { static void Main() { List<Shape> shapes = new List<Shape>(); shapes.Add(new Rectangle(10f, 20f)); shapes.Add(new Square(10f)); shapes.Add(new Circle(10f)); foreach (Shape shape in shapes) { Console.WriteLine(shape.getArea()); } } }
Tout était clair ?
Merci pour vos commentaires !
Section 5. Chapitre 5
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion