接口(C# 参考)interface (C# Reference)

接口只包含方法属性事件索引器的签名。An interface contains only the signatures of methods, properties, events or indexers. 实现接口的类或结构必须实现接口定义中指定的接口成员。A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition. 在以下示例中,类 ImplementationClass 必须实现一个不含参数但返回 void 的名为 SampleMethod 的方法。In the following example, class ImplementationClass must implement a method named SampleMethod that has no parameters and returns void.

有关详细信息和示例,请参阅接口For more information and examples, see Interfaces.

示例Example

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();
    }
}

接口可以是命名空间或类的成员,并且可以包含下列成员的签名:An interface can be a member of a namespace or a class and can contain signatures of the following members:

一个接口可从一个或多个基接口继承。An interface can inherit from one or more base interfaces.

基类型列表包含基类和接口时,基类必须是列表中的第 1 项。When a base type list contains a base class and interfaces, the base class must come first in the list.

实现接口的类可以显式实现该接口的成员。A class that implements an interface can explicitly implement members of that interface. 显式实现的成员不能通过类实例访问,而只能通过接口实例访问。An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.

有关显式接口实现的更多详细信息和代码示例,请参阅显式接口实现For more details and code examples on explicit interface implementation, see Explicit Interface Implementation.

示例Example

下例演示了接口实现。The following example demonstrates interface implementation. 在此示例中,接口包含属性声明,类包含实现。In this example, the interface contains the property declaration and the class contains the implementation. 实现 IPoint 的类的任何实例都具有整数属性 xyAny instance of a class that implements IPoint has integer properties x and y.

interface IPoint
{
   // Property signatures:
   int x
   {
      get;
      set;
   }

   int y
   {
      get;
      set;
   }
}

class Point : IPoint
{
   // Fields:
   private int _x;
   private int _y;

   // Constructor:
   public Point(int x, int y)
   {
      _x = x;
      _y = y;
   }

   // Property implementation:
   public int x
   {
      get
      {
         return _x;
      }

      set
      {
         _x = value;
      }
   }

   public int y
   {
      get
      {
         return _y;
      }
      set
      {
         _y = value;
      }
   }
}

class MainClass
{
   static void PrintPoint(IPoint p)
   {
      Console.WriteLine("x={0}, y={1}", p.x, p.y);
   }

   static void Main()
   {
      IPoint p = new Point(2, 3);
      Console.Write("My Point: ");
      PrintPoint(p);
   }
}
// Output: My Point: x=2, y=3

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also