Oefening 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768using 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(); } }
- Voeg het sleutelwoord
abstracttoe vóór de definitie van de klasseAnimalom deze abstract te maken. - Voeg het sleutelwoord
abstracttoe vóór het returntype van de methodemakeSoundin de klasseAnimalom de methode abstract te maken. Zorg ervoor dat je de methodebody uit de klasseAnimalverwijdert, zodat alleen de blauwdruk overblijft (returntype, naam en argumenten):returnType methodName(arg1, arg2, ..);
index.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465using 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(); } }
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
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
Oefening 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768using 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(); } }
- Voeg het sleutelwoord
abstracttoe vóór de definitie van de klasseAnimalom deze abstract te maken. - Voeg het sleutelwoord
abstracttoe vóór het returntype van de methodemakeSoundin de klasseAnimalom de methode abstract te maken. Zorg ervoor dat je de methodebody uit de klasseAnimalverwijdert, zodat alleen de blauwdruk overblijft (returntype, naam en argumenten):returnType methodName(arg1, arg2, ..);
index.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465using 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(); } }
Bedankt voor je feedback!