Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Sfida: Costruttori | Introduzione alla Programmazione Orientata agli Oggetti (OOP)
C# Oltre le Basi

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

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

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

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(); } }
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 10

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

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

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

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

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

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(); } }
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 10
some-alt