C # understanding inheritance

1

I got a problem understanding inheritance, I have my superclass "servivo" that has a constructor which receives a string, which would be the name of the servivo, besides this it has a method called "breathe" which prints on the screen string + "breathe." Ok then I create my human subclass that contains the "I speak" method that prints on the screen string + I speak. Then the problem arises when I instancio an object of the human class the why not ... how to add the string parameter that would have to receive servivo, that is, I do not know if you can place a parameter that received the super class.

QUESTION "servivo" needs a parameter string, so when I want to instantiate a "human" (subclass of servivo) and I want to pass the parameter string, so that it is assigned to the variable declared and requested by the "servivo" constructor as I do it, bah if you can ...

   humano A = new humano("shiki") ;

        A.respira();
        A.hablo();



        Console.ReadKey();

    }
}
class servivo
{
    public string var="default";
    public void respira()
    {
        Console.WriteLine("{0} respira",var );

    }
    public servivo(string var2)
    {
        this.var = var2;
    }
}
class humano:servivo
{


    public void hablo()
    {
        Console.WriteLine("{0} habla",var);
    }
    //RESPUESTA COMPLETA

    public humano(string auxiliar) : base(auxiliar)
    {
        this.var = auxiliar;
    }

Only to get rid of doubts probe the code of those two ways and it worked but I enter the doubt and before the doubt I ask, when I put public humano(string auxiliar) : base(auxiliar) { } I am saying with this constructor of subclass and this parameter, I use base and I say pass it as Parameter to my super class?

Oh and thanks again for the help! I read the base doc marked and punctuated.

    public humano(string auxiliar) : base(auxiliar) 
{
 this.var = auxiliar;
 } 
public humano(string auxiliar) : base(auxiliar) 
{ } 
    
asked by Shiki 09.05.2017 в 01:23
source

1 answer

5

The answer you want:

  

Simply perform:

class humano : servivo 
   {
       // ....
       public humano(string nombre) : base(nombre)
       {
           // Resto de tu logica ...
       }
   }
     

Basically it's like applying inheritance to the methods or functions, in that fragment of code what it does is that when calling the constructor of the class humano , the constructor of your "superclass" is called before doing the rest of the constructor actions of humano .

But, so you can understand the inheritance a bit:

  • Inheritance: It is the property of the object-oriented programming (POO) that in part allows the modification of the behavior of an object.

That's the concept you've understood well so far.

Suppose we have the following class:

public class Cosa
{
    public Cosa(string nombre)
    {
        Nombre = nombre;
    }
    public string Nombre { get; private set; }
}

We define the constructor of the class Cosa and we have a common property called Nombre , but, the class is not able to do anything by itself, it is then that we proceed to inherit from the classes like this, inherit it :

public class Televisor : Cosa 
{
    public int Canal { get; set; }
    public Televisor() : base("Televisor")
    {
        Canal = 1; // Ajustamos el TV para estar en el canal 1.
    }
}

In this example I have done the same as in the case that you ask, if you notice, in the constructor of Televisor() I have put : base("Televisor") next to it, this is so that the constructor of Cosa(string) is called before to perform the actions of the constructor of the class Television , as mentioned above, is how to apply the inheritance to a function; when using the keyword base , you can specify any method that produces the behavior of the main class, so with a base.NombreDelMetodo() it is enough to do it in any other method.

If we do the following in the constructor of Television :

public Television() : base("Television")
{
    // resto del codigo
    System.Console.WriteLine(Nombre); 
}

Print "Television" , because we already set the constructor for the class mother , base or "superclass" (whatever you want to call it) to carry out the necessary actions.

Within this field, C # includes abstract classes and virtual functions, with the aim that you can make your object even more abstract, but everything depends on what you need.

    
answered by 09.05.2017 / 01:51
source