Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Utmaning: Arv | OOP-principer
C# Bortom Grunderna

bookUtmaning: Arv

Fyll i luckorna för att säkerställa att baskonstruktörerna anropas korrekt i följande kod:

index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
using System; class Animal { string species; public Animal(string species) { this.species = species; } public void Eat() { Console.WriteLine("The animal is eating."); } } class Dog : Animal { string breed; public Dog(string species, string breed) : ___ { this.breed = breed; } public void Bark() { Console.WriteLine("Woof! Woof!"); } } class GermanShepherd : Dog { public GermanShepherd(string species, string breed) : ___ { Console.WriteLine("GermanShepherd Object Created"); } public void PerformGuardDuty() { Console.WriteLine("The German Shepherd is performing guard duty."); } } class ConsoleApp { static void Main() { new GermanShepherd("Canis lupus familiaris", "German Shepherd"); } }

Använd syntaxen public className(arg1, arg2..) : base(arg1, arg2, …) i de ärvda klasserna för att anropa baskonstruktörerna.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
using System; class Animal { string species; public Animal(string species) { this.species = species; } public void Eat() { Console.WriteLine("The animal is eating."); } } class Dog : Animal { string breed; public Dog(string species, string breed) : base(species) { this.breed = breed; } public void Bark() { Console.WriteLine("Woof! Woof!"); } } class GermanShepherd : Dog { public GermanShepherd(string species, string breed) : base(species, breed) { Console.WriteLine("GermanShepherd Object Created"); } public void PerformGuardDuty() { Console.WriteLine("The German Shepherd is performing guard duty."); } } class ConsoleApp { static void Main() { new GermanShepherd("Canis lupus familiaris", "German Shepherd"); } }
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you provide the code that needs to be completed?

Can you give an example of a base and derived class for this scenario?

Do you want an explanation of how constructor chaining works in C#?

Awesome!

Completion rate improved to 2.04

bookUtmaning: Arv

Svep för att visa menyn

Fyll i luckorna för att säkerställa att baskonstruktörerna anropas korrekt i följande kod:

index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
using System; class Animal { string species; public Animal(string species) { this.species = species; } public void Eat() { Console.WriteLine("The animal is eating."); } } class Dog : Animal { string breed; public Dog(string species, string breed) : ___ { this.breed = breed; } public void Bark() { Console.WriteLine("Woof! Woof!"); } } class GermanShepherd : Dog { public GermanShepherd(string species, string breed) : ___ { Console.WriteLine("GermanShepherd Object Created"); } public void PerformGuardDuty() { Console.WriteLine("The German Shepherd is performing guard duty."); } } class ConsoleApp { static void Main() { new GermanShepherd("Canis lupus familiaris", "German Shepherd"); } }

Använd syntaxen public className(arg1, arg2..) : base(arg1, arg2, …) i de ärvda klasserna för att anropa baskonstruktörerna.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
using System; class Animal { string species; public Animal(string species) { this.species = species; } public void Eat() { Console.WriteLine("The animal is eating."); } } class Dog : Animal { string breed; public Dog(string species, string breed) : base(species) { this.breed = breed; } public void Bark() { Console.WriteLine("Woof! Woof!"); } } class GermanShepherd : Dog { public GermanShepherd(string species, string breed) : base(species, breed) { Console.WriteLine("GermanShepherd Object Created"); } public void PerformGuardDuty() { Console.WriteLine("The German Shepherd is performing guard duty."); } } class ConsoleApp { static void Main() { new GermanShepherd("Canis lupus familiaris", "German Shepherd"); } }
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 3
some-alt