Udfordring: Afledte Klasser
En klasse kaldet Vehicle
er givet. Derudover er der to andre klasser kaldet Car
og Plane
.
Der er i øjeblikket en fejl i koden. For at rette fejlen skal du få Car
og Plane
til at arve fra Vehicle
.
Rediger koden, så Car
og Plane
bliver underklasser af Vehicle
.
index.cs
12345678910111213141516171819202122232425262728293031323334353637using System; public class Vehicle { public int modelYear; public float fuel; } public class Car { public void Drive() { Console.WriteLine("The car is now driving."); } } public class Plane { public void Fly() { Console.WriteLine("The plane is now flying."); } } public class Program { public static void Main(string[] args) { Car c1 = new Car(); Plane p1 = new Plane(); c1.modelYear = 2024; p1.fuel = 6000; Console.WriteLine("Executed Successfully"); } }
Brug symbolet :
i syntaksen for en afledt klasse for at angive forældreklassen.
index.cs
12345678910111213141516171819202122232425262728293031323334353637using System; public class Vehicle { public int modelYear; public float fuel; } public class Car : Vehicle { public void Drive() { Console.WriteLine("The car is now driving."); } } public class Plane : Vehicle { public void Fly() { Console.WriteLine("The plane is now flying."); } } public class Program { public static void Main(string[] args) { Car c1 = new Car(); Plane p1 = new Plane(); c1.modelYear = 2024; p1.fuel = 6000; Console.WriteLine("Executed Successfully"); } }
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you show me the code for the Vehicle, Car, and Plane classes?
What does the error message say?
Can you explain how inheritance works in this context?
Awesome!
Completion rate improved to 2.04
Udfordring: Afledte Klasser
Stryg for at vise menuen
En klasse kaldet Vehicle
er givet. Derudover er der to andre klasser kaldet Car
og Plane
.
Der er i øjeblikket en fejl i koden. For at rette fejlen skal du få Car
og Plane
til at arve fra Vehicle
.
Rediger koden, så Car
og Plane
bliver underklasser af Vehicle
.
index.cs
12345678910111213141516171819202122232425262728293031323334353637using System; public class Vehicle { public int modelYear; public float fuel; } public class Car { public void Drive() { Console.WriteLine("The car is now driving."); } } public class Plane { public void Fly() { Console.WriteLine("The plane is now flying."); } } public class Program { public static void Main(string[] args) { Car c1 = new Car(); Plane p1 = new Plane(); c1.modelYear = 2024; p1.fuel = 6000; Console.WriteLine("Executed Successfully"); } }
Brug symbolet :
i syntaksen for en afledt klasse for at angive forældreklassen.
index.cs
12345678910111213141516171819202122232425262728293031323334353637using System; public class Vehicle { public int modelYear; public float fuel; } public class Car : Vehicle { public void Drive() { Console.WriteLine("The car is now driving."); } } public class Plane : Vehicle { public void Fly() { Console.WriteLine("The plane is now flying."); } } public class Program { public static void Main(string[] args) { Car c1 = new Car(); Plane p1 = new Plane(); c1.modelYear = 2024; p1.fuel = 6000; Console.WriteLine("Executed Successfully"); } }
Tak for dine kommentarer!