Sfida: Costruttori
È fornita una semplice classe chiamata Dog
. Creare un costruttore che accetti come argomenti name
, breed
, age
e inizializzi i campi con i valori degli argomenti.
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(); } }
Per assegnare i valori degli argomenti ai campi senza generare errori, sarà necessario utilizzare l'operatore this
, poiché gli argomenti hanno lo stesso nome dei campi.
index.cs
12345678910111213141516171819202122232425262728using 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(); } }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
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
Sfida: Costruttori
Scorri per mostrare il menu
È fornita una semplice classe chiamata Dog
. Creare un costruttore che accetti come argomenti name
, breed
, age
e inizializzi i campi con i valori degli argomenti.
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(); } }
Per assegnare i valori degli argomenti ai campi senza generare errori, sarà necessario utilizzare l'operatore this
, poiché gli argomenti hanno lo stesso nome dei campi.
index.cs
12345678910111213141516171819202122232425262728using 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(); } }
Grazie per i tuoi commenti!