不同类实现一个接口的示例
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();
}
}
}
}
}
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();
}
}
}
}
}
相关文章推荐
- 一个类如何实现两个接口中同名同参数不同返回值的函数
- 一个类所实现的两个不同的接口中,有同名的方法,怎么知道实现的是哪个接口中的方法
- 仔细思考一个接口的不同实现得到的类和一个类的不同对象使用起来到底有什么区别?
- Flex中实现对一个text渲染不同的字体颜色示例
- 在.NET Core中处理一个接口多个不同实现的依赖注入问题
- 在.NET Core中处理一个接口多个不同实现的依赖注入问题
- 封装的一个sorted_vector示例,实现了stl::set的一部分接口
- 内部类大展身手 ———— 多个内部类以不同的方式实现同一个接口,或继承同一个类
- 一个类如何实现两个接口中同名同参数不同返回值的函数
- Jmeter使用_time函数实现同一个接口参数传入可以每次传入不同的参数
- 一个登录框实现不同的登录验证
- 实例化一个子类对象赋给父类对象引用或一个实现了接口的类的对象赋给接口的引用的好处
- Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格
- 实现一个label中的文本有不同的文字颜色
- 编程:用java 语言实现,输入一个数,就相应地输出的几维数组!||用1、2、2、3、4、5这六个数字,用java写一个函数,打印出所有不同的排列,要求:"4"不能在第三位,"3"与"5"不能相连
- 内部类的作用?1、抽象类中包含一个内部接口如何实现与调用 ;2、接口中包含一个内部抽象类如何调用
- 完美实现在同一个页面中使用不同样式的artDialog样式
- WebSocket不同版本的三种握手方式以及一个Netty实现JAVA类
- 查找返回一个接口的所有实现类
- 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程