C#接口

本文在于巩固基础知识

接口的概念:

接口只包含方法,属性,事件或索引器的签名。 实现接口的类或结构必须实现接口定义中指定的接口成员。

参照链接:

http://www.cnblogs.com/michaelxu/archive/2007/03/29/692021.html

MSDN中提供了一个简单的例子

interface ISampleInterface
{
    void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
    // Explicit interface member implementation: 
    void ISampleInterface.SampleMethod()
    {
        // Method implementation.
    }

    static void Main()
    {
        // Declare an interface instance.
        ISampleInterface obj = new ImplementationClass();

        // Call the member.
        obj.SampleMethod();
    }
}

在这里,接口中只申明了一个方法签名,ImplementClass实现类继承这个接口,意味着这个实现类必须去实现这个接口里面的所有的成员

最后在调用的时候声明了一个接口实例,将从接口派生的对象赋值给接口类型,最后由这个接口实例去执行派生类的方法

原文地址:https://www.cnblogs.com/jixinyu/p/4298692.html