我正在尝试了解如何在 C# 中使用或访问多个类,有人可以向我解释这段代码的作用吗?
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 Eat(Food food)
什么?我的意思是它有什么作用??
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");
}
}
}