平时做程序并不怎么使用接口,现在温习一下,以便之后要用。
示例1:
using System;
namespace ConsoleApplication1
{
interface ICame
{
void Come();
}
interface ISaw
{
void Look();
}
public class Test:ICame,ISaw
{
public void Come()
{
Console.Write("Come/n");
}
public void Look()
{
Console.Write("Look/n");
}
}
public class Test1:ICame,ISaw
{
public void Come()
{
Console.Write("Come1/n");
}
public void Look()
{
Console.Write("Look1/n");
}
}
class Class1
{
void func(ICame ic)
{
ic.Come();
}
static void Main(string[] args)
{
Test t=new Test();
Test1 t1=new Test1();
Class1 c1=new Class1();
c1.func(t);//并不关心它所传递的对象类型
c1.func(t1);//并不关心它所传递的对象类型
//ICame gg;
//gg.Come(); //此处会显示错误《使用了未赋值的局部变量“gg”》,因为ICame未实例化
}
}
}