您的位置:首页 > 编程语言 > C#

C# 接口

2013-10-30 20:13 183 查看
using System;

class testInterface
{
//鸟
class Bird
{
public void Run()
{
Console.WriteLine("鸟在奔跑!");
}
}

//接口
public interface IFlyable
{
//接口和抽象类一样,也是只能有方法的声明,不能有任何的实现
void Fly();
}

//麻雀
class Sparrow : Bird, IFlyable
{
#region IFlyable 成员
public void Fly()
{
Console.WriteLine("小麻雀飞在树林中。");
}
#endregion
}

//鹦鹉
class Parrot : Bird, IFlyable
{
#region IFlyable 成员
public void Fly()
{
Console.WriteLine("鹦鹉在小笼子里飞...");
}
#endregion
}

//企鹅
class Penguin : Bird
{
}

static void Main()
{
IFlyable fly = new Parrot();
fly.Fly();
}
}


Output:

鹦鹉在小笼子里飞...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: