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

bookUtmaning: Härledda Klasser

En klass kallad Vehicle är given. Förutom denna finns det två andra klasser, Car och Plane.

Det finns för närvarande ett fel i koden. För att åtgärda felet behöver du göra så att Car och Plane ärver från Vehicle.

Redigera koden så att Car och Plane blir barnklasser till 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"); } }

Använd symbolen : i syntaxen för en härledd klass för att ange basklassen.

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 allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 2

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

bookUtmaning: Härledda Klasser

Svep för att visa menyn

En klass kallad Vehicle är given. Förutom denna finns det två andra klasser, Car och Plane.

Det finns för närvarande ett fel i koden. För att åtgärda felet behöver du göra så att Car och Plane ärver från Vehicle.

Redigera koden så att Car och Plane blir barnklasser till 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"); } }

Använd symbolen : i syntaxen för en härledd klass för att ange basklassen.

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 allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 2
some-alt