Practice: Constructors
A simple class called Dog
is given. Create a constructor which takes in the arguments name
, breed
, age
, and initializes the fields from the values of arguments.
index.cs
123456789101112131415161718192021222324using 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(); } }
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 3. Luku 10
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Suggested prompts:
Can you explain how the constructor works in this example?
What does the 'this' keyword do in the constructor?
Can you show how to create another Dog object with different values?
Awesome!
Completion rate improved to 2.04
Practice: Constructors
Pyyhkäise näyttääksesi valikon
A simple class called Dog
is given. Create a constructor which takes in the arguments name
, breed
, age
, and initializes the fields from the values of arguments.
index.cs
123456789101112131415161718192021222324using 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(); } }
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 3. Luku 10