Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Haaste: Konstruktorit | Johdatus Olio-Ohjelmointiin (OOP)
Quizzes & Challenges
Quizzes
Challenges
/
C# Perusteiden Jälkeen

bookHaaste: Konstruktorit

Annetaan yksinkertainen luokka nimeltä Dog. Luo konstruktori, joka ottaa argumentit name, breed, age ja alustaa kentät argumenttien arvoilla.

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(); } }

Jotta voit asettaa argumenttien arvot kenttiin ilman virhettä, sinun tulee käyttää this-operaattoria, koska argumenttien nimet ovat samat kuin kenttien nimet.

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(); } }
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 10

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookHaaste: Konstruktorit

Pyyhkäise näyttääksesi valikon

Annetaan yksinkertainen luokka nimeltä Dog. Luo konstruktori, joka ottaa argumentit name, breed, age ja alustaa kentät argumenttien arvoilla.

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(); } }

Jotta voit asettaa argumenttien arvot kenttiin ilman virhettä, sinun tulee käyttää this-operaattoria, koska argumenttien nimet ovat samat kuin kenttien nimet.

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(); } }
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 10
some-alt