您的位置:首页 > 其它

不同类实现一个接口的示例

2011-03-13 13:53 316 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
interface ILiveBirth
{
string BabyCalled();//接口中的方法,返回string类型

}
class Animal { }
class Cat : Animal, ILiveBirth
{
string ILiveBirth.BabyCalled()
{ return "kitten"; }
}
class Dog : Animal, ILiveBirth
{
string ILiveBirth.BabyCalled()
{ return "wangwang"; }
}
class Bird : Animal,ILiveBirth
{
string ILiveBirth.BabyCalled()
{ return "我是一只小小鸟"; }
}
class Program
{
static void Main()
{
Animal[] animalArray = new Animal[3];//创建Animal数组
animalArray[0] = new Cat();
animalArray[1] = new Bird();
animalArray[2] = new Dog();
foreach (var a in animalArray)
{
ILiveBirth b = a as ILiveBirth;
if (b!=null)
{
Console.WriteLine("Baby is called:{0}", b.BabyCalled());
Console.ReadKey();
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐