interface
interface Ishirou
{
void Hunt();
}
interface Ishicao
{
void EatGrass();
}
abstract class Animal
{
public abstract void Eat();
}
class Cat : Animal,Ishirou
{
public override void Eat() { Console.WriteLine("猫吃鱼"); }
public void Hunt()
{
Console.WriteLine("猫捉老鼠");
}
}
class Tiger : Animal, Ishirou
{
public override void Eat() { Console.WriteLine("老虎也吃鱼"); }
public void Hunt()
{
Console.WriteLine("老虎捉羊");
}
}
class Sheep : Animal, Ishicao
{
public override void Eat() { Console.WriteLine("羊吃草"); }
public void EatGrass()
{
}
}
class Program
{
static void Main(string[] args)
{
List<Animal> animals = new List<Animal>();
Animal a1 = new Cat();
animals.Add(a1);
Animal a2 = new Sheep();
animals.Add(a2);
Animal a3 = new Sheep();
animals.Add(a3);
Animal a4 = new Tiger();
animals.Add(a4);
foreach (Animal a in animals)
{
if (a is Ishirou)
{
Ishirou irou = (Ishirou)a;
irou.Hunt();
}
}
}
}
{
void Hunt();
}
interface Ishicao
{
void EatGrass();
}
abstract class Animal
{
public abstract void Eat();
}
class Cat : Animal,Ishirou
{
public override void Eat() { Console.WriteLine("猫吃鱼"); }
public void Hunt()
{
Console.WriteLine("猫捉老鼠");
}
}
class Tiger : Animal, Ishirou
{
public override void Eat() { Console.WriteLine("老虎也吃鱼"); }
public void Hunt()
{
Console.WriteLine("老虎捉羊");
}
}
class Sheep : Animal, Ishicao
{
public override void Eat() { Console.WriteLine("羊吃草"); }
public void EatGrass()
{
}
}
class Program
{
static void Main(string[] args)
{
List<Animal> animals = new List<Animal>();
Animal a1 = new Cat();
animals.Add(a1);
Animal a2 = new Sheep();
animals.Add(a2);
Animal a3 = new Sheep();
animals.Add(a3);
Animal a4 = new Tiger();
animals.Add(a4);
foreach (Animal a in animals)
{
if (a is Ishirou)
{
Ishirou irou = (Ishirou)a;
irou.Hunt();
}
}
}
}