Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Завдання: Поліморфізм | Принципи ООП
C# Понад Базовий Рівень

bookЗавдання: Поліморфізм

Наведена нижче програма містить суперклас під назвою Shape та три похідні класи: Rectangle, Square і Circle. Проблема коду полягає в тому, що методи не були коректно перевизначені, оскільки вивід показує 0.0 для всіх трьох операторів Console.WriteLine, що свідчить про виконання методів базового класу замість методів похідних класів.

Відредагуйте код так, щоб два методи — getArea і calculatePerimeter — були правильно перевизначені.

index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
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() { 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()); } } }
  1. У базовому класі використовуйте ключове слово virtual, щоб зробити методи такими, що можуть бути перевизначені.
  2. У кожному похідному класі додайте ключове слово override у визначеннях методів.
index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
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 virtual float getArea() { return 0.0f; } protected virtual 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 override float getArea() { return width * height; } protected override 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 override float getArea() { return length * length; } protected override 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 override float getArea() { return 3.14f * radius * radius; } protected override 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()); } } }
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 5

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you show me the original code so I can help edit it?

Can you explain what the expected output should be after fixing the code?

Do you need an explanation of how `virtual` and `override` work in C#?

Awesome!

Completion rate improved to 2.04

bookЗавдання: Поліморфізм

Свайпніть щоб показати меню

Наведена нижче програма містить суперклас під назвою Shape та три похідні класи: Rectangle, Square і Circle. Проблема коду полягає в тому, що методи не були коректно перевизначені, оскільки вивід показує 0.0 для всіх трьох операторів Console.WriteLine, що свідчить про виконання методів базового класу замість методів похідних класів.

Відредагуйте код так, щоб два методи — getArea і calculatePerimeter — були правильно перевизначені.

index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
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() { 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()); } } }
  1. У базовому класі використовуйте ключове слово virtual, щоб зробити методи такими, що можуть бути перевизначені.
  2. У кожному похідному класі додайте ключове слово override у визначеннях методів.
index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
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 virtual float getArea() { return 0.0f; } protected virtual 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 override float getArea() { return width * height; } protected override 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 override float getArea() { return length * length; } protected override 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 override float getArea() { return 3.14f * radius * radius; } protected override 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()); } } }
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 5
some-alt