Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Oefening Abstractie | OOP-Principes
C# Verder dan de Basis

bookOefening Abstractie

In deze opdracht krijg je code die je mogelijk al kent, aangezien je vergelijkbare code hebt bekeken in meerdere voorgaande hoofdstukken. Het bevat een overschreven methode genaamd MakeSound.

Je opdracht is om de klasse Animal om te zetten naar een abstracte klasse en de methode MakeSound om te zetten naar een abstract methode.

Na deze aanpassing mag er geen verandering zijn in de uitvoer van het programma.

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. Voeg het sleutelwoord abstract toe vóór de definitie van de klasse Animal om deze abstract te maken.
  2. Voeg het sleutelwoord abstract toe vóór het returntype van de methode makeSound in de klasse Animal om de methode abstract te maken. Zorg ervoor dat je de methodebody uit de klasse Animal verwijdert, zodat alleen de blauwdruk overblijft (returntype, naam en argumenten): 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(); } }
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 5. Hoofdstuk 9

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Awesome!

Completion rate improved to 2.04

bookOefening Abstractie

Veeg om het menu te tonen

In deze opdracht krijg je code die je mogelijk al kent, aangezien je vergelijkbare code hebt bekeken in meerdere voorgaande hoofdstukken. Het bevat een overschreven methode genaamd MakeSound.

Je opdracht is om de klasse Animal om te zetten naar een abstracte klasse en de methode MakeSound om te zetten naar een abstract methode.

Na deze aanpassing mag er geen verandering zijn in de uitvoer van het programma.

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. Voeg het sleutelwoord abstract toe vóór de definitie van de klasse Animal om deze abstract te maken.
  2. Voeg het sleutelwoord abstract toe vóór het returntype van de methode makeSound in de klasse Animal om de methode abstract te maken. Zorg ervoor dat je de methodebody uit de klasse Animal verwijdert, zodat alleen de blauwdruk overblijft (returntype, naam en argumenten): 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(); } }
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 5. Hoofdstuk 9
some-alt