您的位置:首页 > 其它

接口,三层中常用的...

2007-03-09 22:30 309 查看
using System;

namespace LearningCSharp
{
//定义接口
interface ICarnivore
{
bool IsHungry
{
get;
}
Animal Hunt();
void Eat(Animal victim);
}
public abstract class Animal
{
public abstract void Sleep();
}
public class Antelope:Animal
{
public override void Sleep()
{

}
}
public class Lion:Animal,Icarnivore
{
public Lion()
{
hungry=true;
}
private bool hungry;
public bool IsHungry
{
get
{
return hungry;
}
}
public Animal Hunt()
{
return new Antelope();
}
public void Eat(Animal prey)
{
Console.WriteLine("Lion is no longer hungry");
}
public override void Sleep()
{

}

public void JoinPride()
{

}
}
class Tester
{
static void Main(string[] args)
{
Lion aLion=new Lion();
Antelope a=new Antelope();
if(aLion.IsHungry)
{
Animal victim=aLion.Hunt();
if(victim!=null)
{
aLion.Eat(victim);
}
}
aLion.JoinPride();
aLion.Sleep();
}
}

}

is 判断是不是实现了接口
as 强制转换并检查是不是有实现..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: