Abstraksjonsøvelse
I denne oppgaven får du utdelt kode du kanskje allerede er kjent med, siden du har sett lignende kode i flere tidligere kapitler. Koden inneholder en overskrevet metode kalt MakeSound
.
Din oppgave er å gjøre Animal
-klassen om til en abstrakt klasse og MakeSound
-metoden til en abstract
metode.
Det skal ikke være noen endring i programmets utdata etter denne modifikasjonen.
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(); } }
- Legg til
abstract
-nøkkelordet foranAnimal
-klassens definisjon for å gjøre den abstrakt. - Legg til
abstract
-nøkkelordet foran returtypen tilmakeSound
-metoden iAnimal
-klassen for å gjøre metoden abstrakt. Sørg for å fjerne metodekroppen fraAnimal
-klassen slik at kun selve signaturen (returtype, navn og argumenter) står igjen: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(); } }
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
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
Abstraksjonsøvelse
Sveip for å vise menyen
I denne oppgaven får du utdelt kode du kanskje allerede er kjent med, siden du har sett lignende kode i flere tidligere kapitler. Koden inneholder en overskrevet metode kalt MakeSound
.
Din oppgave er å gjøre Animal
-klassen om til en abstrakt klasse og MakeSound
-metoden til en abstract
metode.
Det skal ikke være noen endring i programmets utdata etter denne modifikasjonen.
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(); } }
- Legg til
abstract
-nøkkelordet foranAnimal
-klassens definisjon for å gjøre den abstrakt. - Legg til
abstract
-nøkkelordet foran returtypen tilmakeSound
-metoden iAnimal
-klassen for å gjøre metoden abstrakt. Sørg for å fjerne metodekroppen fraAnimal
-klassen slik at kun selve signaturen (returtype, navn og argumenter) står igjen: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(); } }
Takk for tilbakemeldingene dine!