Conteúdo do Curso
C# Beyond Basics
2. Estruturas & Enumeradores
C# Beyond Basics
Prática: Construtores
Uma classe simples chamada Dog
é fornecida. Crie um construtor que receba os argumentos name
, breed
, age
e inicialize os campos a partir dos valores dos argumentos.
index.cs
1. In order to assign the argument values to the fields without raising any error you'll have to use
this
operator, since the arguments are the same name as the fields.
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();
}
}
Tudo estava claro?
Seção 3. Capítulo 10
Conteúdo do Curso
C# Beyond Basics
2. Estruturas & Enumeradores
C# Beyond Basics
Prática: Construtores
Uma classe simples chamada Dog
é fornecida. Crie um construtor que receba os argumentos name
, breed
, age
e inicialize os campos a partir dos valores dos argumentos.
index.cs
1. In order to assign the argument values to the fields without raising any error you'll have to use
this
operator, since the arguments are the same name as the fields.
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();
}
}
Tudo estava claro?
Seção 3. Capítulo 10