标签:
【访问修饰符】 interface 接口名{}
1、接口完全是定义了一些约定,用来被实现的,所有成员默认是public
2、接口只有方法、属性、索引或事件的声明,没有实现体;
3、可以弥补类的【单一继承】性
4、子类实现接口成员,与抽象类相比,不用使用override重写关键字
public interface IRunable
{
void Run();
}
public class Dog:IRunable
{
public void Run()
{
throw new NotImplementedException();
}
}
5、显示实现接口成员,调用此实现的接口成员时必须通过接口类型的变量来调用,通过实现类变量的调用不能通过编译
public class Dog:IRunable
{
void IRunable.Run()
{
Console.WriteLine("run");
}
}
//调用
IRunable ondog = new Dog();
ondog.Run();//编译正确
Dog ondog = new Dog();
ondog.Run();//编译错误
标签:
原文地址:http://www.cnblogs.com/mingjia/p/4559716.html