Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Udfordring: Afledte Klasser | OOP-Grundprincipper
C# Ud Over Det Grundlæggende

bookUdfordring: 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

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637
using 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

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637
using 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"); } }
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

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

bookUdfordring: 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

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637
using 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

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637
using 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"); } }
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 2
some-alt