I started to practice a bit with the POO, because I do not master the stage very much and I wanted to put into practice some knowledge that I have. Make a small game where a monkey is given to eat fruit (Parent class), among these fruits you have Banana and Lemon (inherited classes), if the monkey ate Banana the monkey reacts differently than how it ate Limon.
The detail that I have now is that I do not know why I can not access the values of flavor and size of the fruit.
In another case I would like to leave the code here and accept suggestions about it to know if it is generally well implemented or if it is not the correct way to do the exercise, especially the way I access it. the types of data to distinguish what the monkey is eating.
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Bienvenido al Mundo de Changos");
Chango chang1 = new Chango();
Banana ban1 = new Banana("Grande","Dulce");
chang1.Eat(ban1);
Chango chang2 = new Chango();
Limon lim1 = new Limon("pequeña","acida");
chang2.Eat(lim1);
}
}
/////// Tipo de alimento
class Fruta
{
private string fsize;
public string size
{
get { return fsize;}
set { fsize = value;}
}
private string fflavor;
public string flavor
{
get { return fflavor;}
set { fflavor = flavor;}
}
public Fruta(string size, string flavor)
{
this.size = size;
this.flavor = flavor;
}
}
////////// Frutas
class Banana : Fruta
{
public Banana(string size, string flavor ):base(size, flavor)
{
}
}
class Limon : Fruta
{
public Limon(string size, string flavor):base(size,flavor)
{
}
}
/////// El Chango
class Chango
{
public void Sad(Fruta fruta){
Console.WriteLine ("El changuito esta triste porque no le gusta " + fruta.GetType()) ;
}
public void Happy(Fruta fruta){
Console.WriteLine ("El changuito esta contento porque le gusta la " + fruta.GetType()) ;
}
public void Taste (Fruta fruta){
Console.WriteLine("El changuito cree que la " + fruta.GetType() + " sabe " +fruta.flavor);
}
public void Eat(Fruta fruta){
Console.WriteLine("El changuito comió " + fruta.GetType().ToString() );
if(fruta.GetType() == Type.GetType("Rextester.Banana"))
{
Happy(fruta);
} else {
Sad(fruta);
}
Taste(fruta);
}
}