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

bookПрактика Абстракції

У цьому завданні надається код, з яким ви вже могли ознайомитися, оскільки подібний код розглядався у кількох попередніх розділах. Він містить перевизначений метод під назвою MakeSound.

Ваше завдання — перетворити клас Animal на абстрактний клас, а метод MakeSound — на abstract метод.

Після цієї модифікації вивід програми не повинен змінитися.

index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
using System; class Animal { protected string species; public Animal(string species) { this.species = species; } public virtual void MakeSound() { // Empty Method } public void DisplaySpecies() { Console.WriteLine($"Species: {species}"); } } class Cat : Animal { string furPattern; public Cat(string species, string furPattern) : base(species) { this.furPattern = furPattern; } public override void MakeSound() { Console.WriteLine("Meow! Meow!"); } } class Dog : Animal { float weight; public Dog(string species, float weight) : base(species) { this.weight = weight; } public override void MakeSound() { Console.WriteLine("Woof! Woof!"); } } class ConsoleApp { static void Main() { Animal myCat = new Cat("Feline", "Ginger & White"); Animal myDog = new Dog("Canine", 42.5f); myCat.DisplaySpecies(); myCat.MakeSound(); Console.WriteLine("\n"); myDog.DisplaySpecies(); myDog.MakeSound(); } }
  1. Додати ключове слово abstract перед визначенням класу Animal, щоб зробити його абстрактним.
  2. Додати ключове слово abstract перед типом, що повертається методом makeSound у класі Animal, щоб зробити метод абстрактним. Необхідно видалити тіло методу з класу Animal, залишивши лише його сигнатуру (тип, ім'я та аргументи): returnType methodName(arg1, arg2, ..);
index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
using System; abstract class Animal { protected string species; public Animal(string species) { this.species = species; } public abstract void MakeSound(); public void DisplaySpecies() { Console.WriteLine($"Species: {species}"); } } class Cat : Animal { string furPattern; public Cat(string species, string furPattern) : base(species) { this.furPattern = furPattern; } public override void MakeSound() { Console.WriteLine("Meow! Meow!"); } } class Dog : Animal { float weight; public Dog(string species, float weight) : base(species) { this.weight = weight; } public override void MakeSound() { Console.WriteLine("Woof! Woof!"); } } class ConsoleApp { static void Main() { Animal myCat = new Cat("Feline", "Ginger & White"); Animal myDog = new Dog("Canine", 42.5f); myCat.DisplaySpecies(); myCat.MakeSound(); Console.WriteLine("\n"); myDog.DisplaySpecies(); myDog.MakeSound(); } }
Все було зрозуміло?

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

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

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

Запитати АІ

expand

Запитати АІ

ChatGPT

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

Suggested prompts:

Can you show me an example of how the abstract class and method should look?

What is the purpose of making the class and method abstract in this context?

Can you explain what changes, if any, need to be made to the subclasses?

Awesome!

Completion rate improved to 2.04

bookПрактика Абстракції

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

У цьому завданні надається код, з яким ви вже могли ознайомитися, оскільки подібний код розглядався у кількох попередніх розділах. Він містить перевизначений метод під назвою MakeSound.

Ваше завдання — перетворити клас Animal на абстрактний клас, а метод MakeSound — на abstract метод.

Після цієї модифікації вивід програми не повинен змінитися.

index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
using System; class Animal { protected string species; public Animal(string species) { this.species = species; } public virtual void MakeSound() { // Empty Method } public void DisplaySpecies() { Console.WriteLine($"Species: {species}"); } } class Cat : Animal { string furPattern; public Cat(string species, string furPattern) : base(species) { this.furPattern = furPattern; } public override void MakeSound() { Console.WriteLine("Meow! Meow!"); } } class Dog : Animal { float weight; public Dog(string species, float weight) : base(species) { this.weight = weight; } public override void MakeSound() { Console.WriteLine("Woof! Woof!"); } } class ConsoleApp { static void Main() { Animal myCat = new Cat("Feline", "Ginger & White"); Animal myDog = new Dog("Canine", 42.5f); myCat.DisplaySpecies(); myCat.MakeSound(); Console.WriteLine("\n"); myDog.DisplaySpecies(); myDog.MakeSound(); } }
  1. Додати ключове слово abstract перед визначенням класу Animal, щоб зробити його абстрактним.
  2. Додати ключове слово abstract перед типом, що повертається методом makeSound у класі Animal, щоб зробити метод абстрактним. Необхідно видалити тіло методу з класу Animal, залишивши лише його сигнатуру (тип, ім'я та аргументи): returnType methodName(arg1, arg2, ..);
index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
using System; abstract class Animal { protected string species; public Animal(string species) { this.species = species; } public abstract void MakeSound(); public void DisplaySpecies() { Console.WriteLine($"Species: {species}"); } } class Cat : Animal { string furPattern; public Cat(string species, string furPattern) : base(species) { this.furPattern = furPattern; } public override void MakeSound() { Console.WriteLine("Meow! Meow!"); } } class Dog : Animal { float weight; public Dog(string species, float weight) : base(species) { this.weight = weight; } public override void MakeSound() { Console.WriteLine("Woof! Woof!"); } } class ConsoleApp { static void Main() { Animal myCat = new Cat("Feline", "Ginger & White"); Animal myDog = new Dog("Canine", 42.5f); myCat.DisplaySpecies(); myCat.MakeSound(); Console.WriteLine("\n"); myDog.DisplaySpecies(); myDog.MakeSound(); } }
Все було зрозуміло?

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

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

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