Cursos /
C# Beyond Basics
Práctica: Constructores
Se da una clase simple llamada Dog
. Crea un constructor que tome los argumentos nombre
, raza
, edad
, e inicialice los campos a partir de los valores de los 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();
}
}
¿Todo estuvo claro?
Sección 3. Capítulo 10
Contenido del Curso
C# Beyond Basics
1. Estructuras Adicionales y Manejo de Archivos
2. Estructuras y Enumeradores
¿Qué son las Structs?Definición y uso de una estructuraPracticando StructsEstructuras con Otras Estructuras de DatosMétodos de EstructuraAcceder Correctamente a los Campos en Métodos de StructMétodos de PrácticaConstructores de EstructuraPracticando ConstructoresEnumeradoresPracticante de Enumerador
3. Introducción a la Programación Orientada a Objetos (POO)
C# Beyond Basics
Práctica: Constructores
Se da una clase simple llamada Dog
. Crea un constructor que tome los argumentos nombre
, raza
, edad
, e inicialice los campos a partir de los valores de los 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();
}
}
¿Todo estuvo claro?
Sección 3. Capítulo 10