I am trying to understand how to use or access multiple classes in C#, can someone explain to me what this code does?
public class Mammal : Animal { public Mammal(String name) : base(name) { } public override void Eat(Food food) { Console.WriteLine("Mammal \"" + Name + "\" eats " + food.Name); } } What is the purpose of public override void Eat(Food food)? I mean what does it do??
namespace ConsoleApplication1 { public class Food { private String name; Food(String name) { this.name = name; } public String Name { get { return name; } set { name = value; } } } public class Animal { private String name = "no name"; public String Name { get { return name; } set { name = value; } } private Animal() { } public Animal(String name) { this.name = name; } public virtual void Eat(Food food) { Console.WriteLine("Animal \"" + Name + "\" eats " + food.Name); } public virtual void Born() { Console.WriteLine("Animal \"" + Name + "\" is born"); } public virtual void Die() { Console.WriteLine("Animal \"" + Name + "\" is died"); } } public class Mammal : Animal { public Mammal(String name) : base(name) { } public override void Eat(Food food) { Console.WriteLine("Mammal \"" + Name + "\" eats " + food.Name); } public override void Born() { Console.WriteLine("Mammal \"" + Name + "\" is born"); } public override void Die() { Console.WriteLine("Mammal \"" + Name + "\" is died"); } public virtual void FedMilk(Mammal[] children) { for (Int32 i = 0; i < children.Length; i++) { Console.WriteLine("Mammal \"" + Name + "\" feds milk child \"" + children[i].Name + "\""); } } } public class Horse : Mammal { public Horse(String name) : base(name) { } public override void Eat(Food food) { Console.WriteLine("Horse \"" + Name + "\" eats " + food.Name); } public override void Born() { Console.WriteLine("Horse \"" + Name + "\" is born"); } } }
Eat()method in your classMammalmeans that you're altering the default behaviour of theEat()method in theAnimalclass. What else are you looking for?