#region Animal 虚基类 有一个name属性 Feed方法和一个虚方法MakeANoise
//虚基类 有一个name属性 Feed方法和一个虚方法MakeANoise
public abstract class Animal
{
protected string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public Animal()
{
name = "The animal with no name";
}
public Animal(string newName)
{
name = newName;
}
public void Feed()
{
Console.WriteLine("{0} has been fed.", name);
}
public abstract void MakeANoise();
}
#endregion
//Cow Animal的子类,实现虚方法
public class Cow:Animal
{
public Cow(string name) :
base(name)
{
}
public override void MakeANoise()
{
Console.WriteLine("{0} says 'moo!'", name);
}
}
//Chicken类,Animal子类
public class Chicken:Animal
{
public Chicken(string name)
: base(name)
{ }
public override void MakeANoise()
{
Console.WriteLine("{0} says 'cluck'", name);
}
}