接口(C# 编程指南)Interfaces (C# Programming Guide)

接口包含非抽象结构必须实现的一组相关功能的定义。An interface contains definitions for a group of related functionalities that a non-abstract class or a struct must implement. 接口可以定义 static 方法,此类方法必须具有实现。An interface may define static methods, which must have an implementation. 从 C# 8.0 开始,接口可为成员定义默认实现。Beginning with C# 8.0, an interface may define a default implementation for members. 接口不能声明实例数据,如字段、自动实现的属性或类似属性的事件。An interface may not declare instance data such as fields, auto-implemented properties, or property-like events.

例如,使用接口可以在类中包括来自多个源的行为。By using interfaces, you can, for example, include behavior from multiple sources in a class. 该功能在 C# 中十分重要,因为该语言不支持类的多重继承。That capability is important in C# because the language doesn't support multiple inheritance of classes. 此外,如果要模拟结构的继承,也必须使用接口,因为它们无法实际从另一个结构或类继承。In addition, you must use an interface if you want to simulate inheritance for structs, because they can't actually inherit from another struct or class.

可使用 interface 关键字定义接口,如以下示例所示。You define an interface by using the interface keyword as the following example shows.

interface IEquatable<T>
{
    bool Equals(T obj);
}

接口名称必须是有效的 C# 标识符名称The name of an interface must be a valid C# identifier name. 按照约定,接口名称以大写字母 I 开头。By convention, interface names begin with a capital I.

实现 IEquatable<T> 接口的任何类或结构都必须包含与该接口指定的签名匹配的 Equals 方法的定义。Any class or struct that implements the IEquatable<T> interface must contain a definition for an Equals method that matches the signature that the interface specifies. 因此,可以依靠实现 IEquatable<T> 的类来包含 Equals 方法,类的实例可以通过该方法确定它是否等于相同类的另一个实例。As a result, you can count on a class that implements IEquatable<T> to contain an Equals method with which an instance of the class can determine whether it's equal to another instance of the same class.

IEquatable<T> 的定义不为 Equals 提供实现。The definition of IEquatable<T> doesn't provide an implementation for Equals. 类或结构可以实现多个接口,但是类只能从单个类继承。A class or struct can implement multiple interfaces, but a class can only inherit from a single class.

有关抽象类的详细信息,请参阅抽象类、密封类及类成员For more information about abstract classes, see Abstract and Sealed Classes and Class Members.

接口可以包含实例方法、属性、事件、索引器或这四种成员类型的任意组合。Interfaces can contain instance methods, properties, events, indexers, or any combination of those four member types. 接口可以包含静态构造函数、字段、常量或运算符。Interfaces may contain static constructors, fields, constants, or operators. 有关示例的链接,请参阅相关部分For links to examples, see Related Sections. 接口不能包含实例字段、实例构造函数或终结器。An interface can't contain instance fields, instance constructors, or finalizers. 默认情况下,接口成员是公共的。Interface members are public by default.

若要实现接口成员,实现类的对应成员必须是公共、非静态,并且具有与接口成员相同的名称和签名。To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.

当类或结构实现接口时,类或结构必须为该接口声明的所有成员提供实现,但不提供默认实现。When a class or struct implements an interface, the class or struct must provide an implementation for all of the members that the interface declares but doesn't provide a default implementation for. 但是,如果基类实现接口,则从基类派生的任何类都会继承该实现。However, if a base class implements an interface, any class that's derived from the base class inherits that implementation.

下面的示例演示 IEquatable<T> 接口的实现。The following example shows an implementation of the IEquatable<T> interface. 实现类 Car 必须提供 Equals 方法的实现。The implementing class, Car, must provide an implementation of the Equals method.

public class Car : IEquatable<Car>
{
    public string Make {get; set;}
    public string Model { get; set; }
    public string Year { get; set; }

    // Implementation of IEquatable<T> interface
    public bool Equals(Car car)
    {
        return (this.Make, this.Model, this.Year) ==
            (car.Make, car.Model, car.Year);
    }
}

类的属性和索引器可以为接口中定义的属性或索引器定义额外的访问器。Properties and indexers of a class can define extra accessors for a property or indexer that's defined in an interface. 例如,接口可能会声明包含 get 取值函数的属性。For example, an interface might declare a property that has a get accessor. 实现此接口的类可以声明包含 getset 取值函数的同一属性。The class that implements the interface can declare the same property with both a get and set accessor. 但是,如果属性或索引器使用显式实现,则访问器必须匹配。However, if the property or indexer uses explicit implementation, the accessors must match. 有关显式实现的详细信息,请参阅显式接口实现接口属性For more information about explicit implementation, see Explicit Interface Implementation and Interface Properties.

接口可从一个或多个接口继承。Interfaces can inherit from one or more interfaces. 派生接口从其基接口继承成员。The derived interface inherits the members from its base interfaces. 实现派生接口的类必须实现派生接口中的所有成员,包括派生接口的基接口的所有成员。A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces. 该类可能会隐式转换为派生接口或任何其基接口。That class may be implicitly converted to the derived interface or any of its base interfaces. 类可能通过它继承的基类或通过其他接口继承的接口来多次包含某个接口。A class might include an interface multiple times through base classes that it inherits or through interfaces that other interfaces inherit. 但是,类只能提供接口的实现一次,并且仅当类将接口作为类定义的一部分 (class ClassName : InterfaceName) 进行声明时才能提供。However, the class can provide an implementation of an interface only one time and only if the class declares the interface as part of the definition of the class (class ClassName : InterfaceName). 如果由于继承实现接口的基类而继承了接口,则基类会提供接口的成员的实现。If the interface is inherited because you inherited a base class that implements the interface, the base class provides the implementation of the members of the interface. 但是,派生类可以重新实现任何虚拟接口成员,而不是使用继承的实现。However, the derived class can reimplement any virtual interface members instead of using the inherited implementation. 当接口声明方法的默认实现时,实现该接口的任何类都会继承该实现。When interfaces declare a default implementation of a method, any class implementing that interface inherits that implementation. 接口中定义的实现是虚拟的,实现类可能会替代该实现。Implementations defined in interfaces are virtual and the implementing class may override that implementation.

基类还可以使用虚拟成员实现接口成员。A base class can also implement interface members by using virtual members. 在这种情况下,派生类可以通过重写虚拟成员来更改接口行为。In that case, a derived class can change the interface behavior by overriding the virtual members. 有关虚拟成员的详细信息,请参阅多态性For more information about virtual members, see Polymorphism.

接口摘要Interfaces summary

接口具有以下属性:An interface has the following properties:

  • 接口通常类似于只有抽象成员的抽象基类。An interface is typically like an abstract base class with only abstract members. 实现接口的任何类或结构都必须实现其所有成员。Any class or struct that implements the interface must implement all its members. 接口可以选择性地定义其部分或全部成员的默认实现。Optionally, an interface may define default implementations for some or all of its members. 有关详细信息,请参阅默认接口方法For more information, see default interface methods.
  • 接口无法直接进行实例化。An interface can't be instantiated directly. 其成员由实现接口的任何类或结构来实现。Its members are implemented by any class or struct that implements the interface.
  • 一个类或结构可以实现多个接口。A class or struct can implement multiple interfaces. 一个类可以继承一个基类,还可实现一个或多个接口。A class can inherit a base class and also implement one or more interfaces.

请参阅See also