Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Utfordring: Konstruktører | Introduksjon til Objektorientert Programmering (OOP)
C# Utover Det Grunnleggende

bookUtfordring: Konstruktører

En enkel klasse kalt Dog er gitt. Lag en konstruktør som tar inn argumentene name, breed, age, og initialiserer feltene med verdiene fra argumentene.

index.cs

index.cs

copy
123456789101112131415161718192021222324
using System; class Dog { public string name; public string breed; public int age; // Write constructor code below this line // Write constructor code above this line public void bark() { Console.WriteLine("Woof!"); } } public class ConsoleApp { public static void Main(string[] args) { Dog dog = new Dog("Dobby", "Dobermann", 4); dog.bark(); } }

For å tilordne argumentverdiene til feltene uten å få en feil, må du bruke this-operatoren, siden argumentene har samme navn som feltene.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728
using System; class Dog { public string name; public string breed; public int age; // Write constructor code below this line public Dog(string name, string breed, int age) { this.name = name; this.breed = breed; this.age = age; } // Write constructor code above this line public void bark() { Console.WriteLine("Woof!"); } } public class ConsoleApp { public static void Main(string[] args) { Dog dog = new Dog("Dobby", "Dobermann", 4); dog.bark(); } }
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 10

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you show me how to write the constructor for the Dog class?

What does the complete Dog class look like with the constructor?

Can you explain how the `this` operator works in this context?

Awesome!

Completion rate improved to 2.04

bookUtfordring: Konstruktører

Sveip for å vise menyen

En enkel klasse kalt Dog er gitt. Lag en konstruktør som tar inn argumentene name, breed, age, og initialiserer feltene med verdiene fra argumentene.

index.cs

index.cs

copy
123456789101112131415161718192021222324
using System; class Dog { public string name; public string breed; public int age; // Write constructor code below this line // Write constructor code above this line public void bark() { Console.WriteLine("Woof!"); } } public class ConsoleApp { public static void Main(string[] args) { Dog dog = new Dog("Dobby", "Dobermann", 4); dog.bark(); } }

For å tilordne argumentverdiene til feltene uten å få en feil, må du bruke this-operatoren, siden argumentene har samme navn som feltene.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728
using System; class Dog { public string name; public string breed; public int age; // Write constructor code below this line public Dog(string name, string breed, int age) { this.name = name; this.breed = breed; this.age = age; } // Write constructor code above this line public void bark() { Console.WriteLine("Woof!"); } } public class ConsoleApp { public static void Main(string[] args) { Dog dog = new Dog("Dobby", "Dobermann", 4); dog.bark(); } }
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 10
some-alt