接口Interfaces

接口定义了可由类和结构实现的协定。An interface defines a contract that can be implemented by classes and structs. 接口可以包含方法、属性、事件和索引器。An interface can contain methods, properties, events, and indexers. 接口不提供所定义的成员的实现代码,仅指定必须由实现接口的类或结构提供的成员。An interface does not provide implementations of the members it defines—it merely specifies the members that must be supplied by classes or structs that implement the interface.

接口可以采用多重继承Interfaces may employ multiple inheritance. 在以下示例中,接口 IComboBox 同时继承自 ITextBoxIListBoxIn the following example, the interface IComboBox inherits from both ITextBox and IListBox.

interface IControl
{
    void Paint();
}
interface ITextBox: IControl
{
    void SetText(string text);
}
interface IListBox: IControl
{
    void SetItems(string[] items);
}
interface IComboBox: ITextBox, IListBox {}

类和结构可以实现多个接口。Classes and structs can implement multiple interfaces. 在以下示例中,类 EditBox 同时实现 IControlIDataBoundIn the following example, the class EditBox implements both IControl and IDataBound.

interface IDataBound
{
    void Bind(Binder b);
}
public class EditBox: IControl, IDataBound
{
    public void Paint() { }
    public void Bind(Binder b) { }
} 

当类或结构实现特定接口时,此类或结构的实例可以隐式转换成相应的接口类型。When a class or struct implements a particular interface, instances of that class or struct can be implicitly converted to that interface type. 例如For example

EditBox editBox = new EditBox();
IControl control = editBox;
IDataBound dataBound = editBox;

如果已知实例不是静态地实现特定接口,可以使用动态类型显式转换功能。In cases where an instance is not statically known to implement a particular interface, dynamic type casts can be used. 例如,以下语句使用动态类型显式转换功能来获取对象的 IControlIDataBound 接口实现代码。For example, the following statements use dynamic type casts to obtain an object’s IControl and IDataBound interface implementations. 因为对象的运行时实际类型是 EditBox,所以显式转换会成功。Because the run-time actual type of the object is EditBox, the casts succeed.

object obj = new EditBox();
IControl control = (IControl)obj;
IDataBound dataBound = (IDataBound)obj;

在前面的 EditBox 类中,IControl 接口中的 Paint 方法和 IDataBound 接口中的 Bind 方法均使用公共成员进行实现。In the previous EditBox class, the Paint method from the IControl interface and the Bind method from the IDataBound interface are implemented using public members. C# 还支持显式接口成员实现代码,这样类或结构就不会将成员设为公共成员。C# also supports explicit interface member implementations, enabling the class or struct to avoid making the members public. 显式接口成员实现代码是使用完全限定的接口成员名称进行编写。An explicit interface member implementation is written using the fully qualified interface member name. 例如,EditBox 类可以使用显式接口成员实现代码来实现 IControl.PaintIDataBound.Bind 方法,如下所示。For example, the EditBox class could implement the IControl.Paint and IDataBound.Bind methods using explicit interface member implementations as follows.

public class EditBox: IControl, IDataBound
{
    void IControl.Paint() { }
    void IDataBound.Bind(Binder b) { }
}

显式接口成员只能通过接口类型进行访问。Explicit interface members can only be accessed via the interface type. 例如,只有先将 EditBox 引用转换成 IControl 接口类型,才能调用上面 EditBox 类提供的 IControl.Paint 实现代码。For example, the implementation of IControl.Paint provided by the previous EditBox class can only be invoked by first converting the EditBox reference to the IControl interface type.

EditBox editBox = new EditBox();
editBox.Paint();            // Error, no such method
IControl control = editBox;
control.Paint();            // Ok