C#中的interface

Interfaces (C# Programming Guide)



Interfaces are defined using the interface keyword. For example:

C#
interface IComparable
{
    int CompareTo(object obj);
}

Interfaces describe a group of related behaviors that can belong to any class or struct. Interfaces can be made up of methods, properties, events, indexers, or any combination of those four member types. An interface can not contain fields. Interfaces members are automatically public.

Classes and structs can inherit from interfaces in a manner similar to how classes can inherit a base class or struct, with two exceptions:

  • A class or struct can inherit more than one interface.

  • When a class or struct inherits an interface, it inherits only the method names and signatures, because the interface itself contains no implementations. For example:

    C#
    public class Minivan : Car, IComparable
    {
        public int CompareTo(object obj)
        {
            //implementation of CompareTo
            return 0;  //if the Minivans are equal
        }
    }
    
    

To implement an interface member, the corresponding member on the class must be public, non-static, and have the same name and signature as the interface member. Properties and indexers on a class can define extra accessors for a property or indexer defined on an interface. For example, an interface may declare a property with a get accessor, but the class implementing 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.

Interfaces and interface members are abstract; interfaces do not provide a default implementation. For more information, see Abstract and Sealed Classes and Class Members.

The IComparable interface announces to the user of the object that the object can compare itself to other objects of the same type, and the user of the interface does not need to know how this is implemented.

Interfaces can inherit other interfaces. It is possible for a class to inherit an interface multiple times, through base classes or interfaces it inherits. In this case, the class can only implement the interface once, if it is declared as part of the new class. If the inherited interface is not declared as part of the new class, its implementation is provided by the base class that declared it. It is possible for a base class to implement interface members using virtual members; in that case, the class inheriting the interface can change the interface behavior by overriding the virtual members. For more information on virtual members, see Polymorphism.

An interface has the following properties:

  • An interface is similar to an abstract base class: any non-abstract type inheriting the interface must implement all its members.

  • An interface cannot be instantiated directly.

  • Interfaces can contain events, indexers, methods and properties.

  • Interfaces contain no implementation of methods.

  • Classes and structs can inherit from more than one interface.

  • An interface can itself inherit from multiple interfaces.

C#interface是一种定义了一组方法、属性和事件的类型。它提供了一种约定,用于指定类应该实现哪些成员。接口的成员只是声明,没有具体的实现。\[1\]通过实现接口,类可以遵循接口的约定,并提供自己的实现。一个类可以实现多个接口,从而具备多个接口所定义的行为。\[2\]在C#,接口的定义规范是使用关键字interface来定义,接口类名称通常以"I"开头。接口的属性、方法等默认都是public,不需要额外指定访问修饰符。\[3\] #### 引用[.reference_title] - *1* [C# Interface 关于接口的多级继承](https://blog.csdn.net/weixin_40695640/article/details/130351587)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C# interface与abstract class区别](https://blog.csdn.net/Fone123123/article/details/125968218)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [C#interface(接口)的定义与实现接口](https://blog.csdn.net/weixin_44548405/article/details/108634772)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值