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)
{ }