ASP.NET接口的基础使用例子

 

using System;
namespace ConsoleApplication
{
internal interface IMyHandler //接口
{
void SayHello();
void SayMsg(string msg);
}
class MyHandlerA : IMyHandler //具体实现类A
{
public void SayHello()
{
Console.WriteLine("Hello from MyHandlerA");
}
public void SayMsg(string msg)
{
Console.WriteLine(msg+" from MyHandlerA");
}
}
class MyHandlerB : IMyHandler //具体实现类B
{
public void SayHello()
{
Console.WriteLine("Hello from MyHandlerB");
}
public void SayMsg(string msg)
{
Console.WriteLine(msg + " from MyHandlerB");
}
}
class Program
{
static void Main(string[] args)
{
//实现化A,用接口类型来声明
IMyHandler myHandler = new MyHandlerA();
myHandler.SayHello();
//实现化B
myHandler = new MyHandlerB();
myHandler.SayHello();
Write(myHandler);
Console.Read();
}
//接口作为参数类型
static void Write(IMyHandler myControl)
{
myControl.SayMsg("Hello World!!!");
}
}
}



posted @ 2012-02-03 16:33  ``炯``  阅读(564)  评论(0编辑  收藏  举报