当前位置: 首页 > news >正文

C#基础类型系统-接口

接口 - 定义多种类型的行为

  • 接口包含非抽象 class 或 struct 必须实现的一组相关功能的定义。
  • 接口可以定义 static 方法,此类方法必须具有实现。
  • 接口可为成员定义默认实现。
  • 接口不能声明实例数据,如字段、自动实现的属性或类似属性的事件。
  • C#不支持类的多重继承,使用接口可以在类中包括来自多个源的行为。
  • 如果要模拟结构的继承,也必须使用接口,因为它们无法实际从另一个结构或类继承。
// 使用 interface 关键字定义接口
// 接口可以包含实例方法、属性、事件、索引器或这四种成员类型的任意组合。 
// 接口可以包含静态构造函数、字段、常量或运算符。
interface IEquatable<T>
{
    bool Equals(T obj);
}
// 接口的实现
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);
    }
}
  • 类的属性和索引器可以为接口中定义的属性或索引器定义额外的访问器。
public interface IExample
{
    // 定义一个只读属性
    int ReadOnlyProperty { get; }

    // 定义一个可读写的属性
    string ReadWriteProperty { get; set; }

    // 定义一个索引器
    int this[int index] { get; set; }
}
using System;

// 概念:接口可从一个或多个接口继承。
public interface IBaseInterface
{
    void BaseMethod(); // 基接口中的方法
}

public interface IDerivedInterface : IBaseInterface
{
    void DerivedMethod(); // 派生接口中的方法
}

// 概念:派生接口从其基接口继承成员。
// IDerivedInterface 继承了 IBaseInterface 的 BaseMethod()。

// 概念:实现派生接口的类必须实现派生接口中的所有成员,
// 包括派生接口的基接口的所有成员。
public class MyClass : IDerivedInterface
{
    public void BaseMethod()
    {
        Console.WriteLine("MyClass: Implementing BaseMethod from IBaseInterface.");
    }

    public void DerivedMethod()
    {
        Console.WriteLine("MyClass: Implementing DerivedMethod from IDerivedInterface.");
    }
}

// 概念:该类可能会隐式转换为派生接口或任何其基接口。
public class Program
{
    public static void Main()
    {
        // 创建类的实例
        MyClass myClass = new MyClass();

        // 隐式转换为派生接口
        IDerivedInterface derivedInterface = myClass;
        derivedInterface.BaseMethod(); // 输出:MyClass: Implementing BaseMethod from IBaseInterface.
        derivedInterface.DerivedMethod(); // 输出:MyClass: Implementing DerivedMethod from IDerivedInterface.

        // 隐式转换为基接口
        IBaseInterface baseInterface = myClass;
        baseInterface.BaseMethod(); // 输出:MyClass: Implementing BaseMethod from IBaseInterface.

        // 概念:类可能通过它继承的基类或通过其他接口继承的接口来多次包含某个接口。
        // 示例:通过基类和接口同时继承同一个接口。
        DerivedClass derivedClass = new DerivedClass();
        derivedClass.BaseMethod(); // 输出:DerivedClass: Overriding BaseMethod.

        // 概念:但是,类只能提供接口的实现一次,并且仅当类将接口作为类定义的一部分进行声明时才能提供。
        // 示例:DerivedClass 重新实现了 BaseMethod。
        IBaseInterface baseInterfaceFromDerived = derivedClass;
        baseInterfaceFromDerived.BaseMethod(); // 输出:DerivedClass: Overriding BaseMethod.

        // 概念:如果由于继承实现接口的基类而继承了接口,则基类会提供接口的成员的实现。
        // 示例:DerivedClass 继承了 MyClass 的实现。
        IDerivedInterface derivedInterfaceFromDerived = derivedClass;
        derivedInterfaceFromDerived.DerivedMethod(); // 输出:MyClass: Implementing DerivedMethod from IDerivedInterface.

        // 概念:当接口声明方法的默认实现时,实现该接口的任何类都会继承该实现。
        // 示例:使用接口的默认实现。
        IDefaultInterface defaultInterface = new DefaultImplementationClass();
        defaultInterface.DefaultMethod(); // 输出:IDefaultInterface: Default implementation of DefaultMethod.
    }
}

// 示例:类可能通过它继承的基类或通过其他接口继承的接口来多次包含某个接口。
public class DerivedClass : MyClass, IBaseInterface
{
    // 概念:派生类可以重新实现任何虚拟接口成员,而不是使用继承的实现。
    public new void BaseMethod()
    {
        Console.WriteLine("DerivedClass: Overriding BaseMethod.");
    }
}

// 概念:当接口声明方法的默认实现时,实现该接口的任何类都会继承该实现。
public interface IDefaultInterface
{
    void DefaultMethod()
    {
        Console.WriteLine("IDefaultInterface: Default implementation of DefaultMethod.");
    }
}

// 实现默认接口的类
public class DefaultImplementationClass : IDefaultInterface
{
    // 不需要显式实现 DefaultMethod,因为它有默认实现。
}

相关文章:

  • Java代码块详解:分类、作用与最佳实践
  • p2p的发展
  • MySQL快速入门
  • 财务管理域——管理会计系统
  • Kotlin 学习-方法和参数类型
  • 「The Road to Web3 Cloud」香港活动回顾|波卡的 Web3 Cloud 愿景
  • winserver2022备份
  • MFC案例:用鼠标移动窗口图像的实验
  • HAL TIM PWM产生 蓝桥杯
  • Java 定时器的全面解析(Timer)
  • Swift Programming All in One苹果程序开发自学之路
  • 【Dify 本地 tools 集成指南】MCP 和 OpenAPI
  • sentinel熔断降级
  • 用Python和OpenCV开启图像处理魔法之旅
  • SmolVLM2: The Smollest Video Model Ever(二)
  • 五种常用的web加密算法
  • 1559 分解质因数
  • 使用Python从零开始构建生成型TransformerLM并训练
  • 高并发场景下的 Java 性能优化
  • 微信小程序开发:废品回收小程序-功能清单