Tutorialsteacher

关注我们

文章
  • C#
  • C# 面向对象编程
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • 控制反转 (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • 正则表达式
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • C# - 入门
  • C# - 版本历史
  • C# - 第一个程序
  • C# - 关键词
  • C# - 类和对象
  • C# - 命名空间
  • C# - 变量
  • C# - 隐式类型变量
  • C# - 数据类型
  • 数字
  • 字符串
  • DateTime
  • 结构体
  • 枚举
  • StringBuilder
  • 匿名类型
  • 动态类型
  • 可空类型
  • C# - 值类型和引用类型
  • C# - 接口
  • C# - 运算符
  • C# - if else 语句
  • C# - 三元运算符 ?
  • C# - Switch 语句
  • C# - For 循环
  • C# - While 循环
  • C# - Do-while 循环
  • C# - 分部类
  • C# - Static 关键字
  • C# - 数组
  • 多维数组
  • 交错数组
  • C# - 索引器
  • C# - 泛型
  • 泛型约束
  • C# - 集合
  • ArrayList
  • List
  • SortedList
  • Dictionary
  • Hashtable
  • Stack
  • Queue
  • C# - 元组
  • C# - 值元组
  • C# - 内置异常
  • 异常处理
  • throw 关键字
  • 自定义异常
  • C# - 委托
  • Func 委托
  • Action 委托
  • Predicate 委托
  • 匿名方法
  • C# - 事件
  • C# - 协变
  • C# - 扩展方法
  • C# - 流 I/O
  • C# - File 类
  • C# - FileInfo 类
  • C# - 对象初始化器
  • OOP - 概述
  • 面向对象编程
  • 抽象
  • 封装
  • 关联与组合
  • 继承
  • 多态
  • 方法重写
  • 方法隐藏
  • C# - SOLID 原则
  • 单一职责原则
  • 开闭原则
  • 里氏替换原则
  • 接口隔离原则
  • 依赖倒置原则
  • 设计模式
  • 单例模式
  • 抽象工厂模式
  • 工厂方法模式
Entity Framework Extensions - 提升 EF Core 9
  批量插入
  批量删除
  批量更新
  批量合并

C# - 接口

在人类世界中,两个人或多人之间的合同约束他们按照合同行事。同样,接口包含相关功能的声明。实现接口的实体必须提供所声明功能的实现。

在 C# 中,可以使用 interface 关键字定义接口。接口可以包含方法、属性、索引器和事件的声明。但是,它不能包含实例字段。

以下接口声明了文件操作的一些基本功能。

示例:C# 接口
interface IFile
{
    void ReadFile();
    void WriteFile(string text);
}

以上声明了一个名为 IFile 的接口。(建议接口名称以字母 "I" 开头,以便容易区分它是接口而不是类。)IFile 接口包含两个方法,ReadFile() 和 WriteFile(string)。

注意
  • 接口可以包含方法、属性、索引器和事件的声明。
  • 从 C# 8.0 开始支持具有实现体的默认接口方法。
  • 接口不能包含构造函数和字段。
  • 接口成员默认是 abstract 和 public。
  • 不能将访问修饰符应用于接口成员。但是,从 C# 8.0 开始,在某些条件下可以使用 private、protected、internal、public、virtual、abstract、sealed、static、extern 和 partial 修饰符。

实现接口

类或结构可以使用冒号 : 实现一个或多个接口。在实现接口时,必须覆盖接口的所有成员。

语法
class ClassName : InterfaceName
{

}

例如,以下 FileInfo 类实现了 IFile 接口,因此它应该覆盖 IFile 的所有成员。

示例:接口实现
interface IFile
{
    void ReadFile();
    void WriteFile(string text);
}

class FileInfo : IFile
{
    public void ReadFile()
    {
        Console.WriteLine("Reading File");
    }

    public void WriteFile(string text)
    {
        Console.WriteLine("Writing to file");
    }
}

在上面的示例中,FileInfo 类实现了 IFile 接口。它使用公共访问修饰符覆盖了 IFile 接口的所有成员。FileInfo 类还可以包含接口成员之外的其他成员。

注意
接口成员必须使用 public 修饰符实现;否则,编译器将给出编译时错误。

您可以创建类的对象并将其分配给接口类型的变量,如下所示。

示例:接口实现
public class Program
{
    public static void Main()
    {
        IFile file1 = new FileInfo();
        FileInfo file2 = new FileInfo();
		
        file1.ReadFile(); 
        file1.WriteFile("content"); 

        file2.ReadFile(); 
        file2.WriteFile("content"); 
    }
}
尝试一下

上面,我们创建了 FileInfo 类的对象,并将其分配给 IFile 类型变量和 FileInfo 类型变量。当接口隐式实现时,您可以使用 IFile 类型变量以及 FileInfo 类型变量访问 IFile 成员。

显式实现

接口可以使用 <InterfaceName>.<MemberName> 进行显式实现。当类实现多个接口时,显式实现非常有用;因此,它更具可读性并消除了混淆。如果接口恰好具有相同的方法名称,它也很有用。

注意
不要在显式实现中使用 public 修饰符。它将导致编译时错误。
示例:显式实现
interface IFile
{
    void ReadFile();
    void WriteFile(string text);
}
    
class FileInfo : IFile
{
    void IFile.ReadFile()
    {
        Console.WriteLine("Reading File");
    }

    void IFile.WriteFile(string text)
    {
        Console.WriteLine("Writing to file");
    }
}

当您显式实现接口时,只能通过接口类型的实例访问接口成员。

示例:显式实现
interface IFile
{
    void ReadFile();
    void WriteFile(string text);
}

class FileInfo : IFile
{
    void IFile.ReadFile()
    {
        Console.WriteLine("Reading File");
    }

    void IFile.WriteFile(string text)
    {
        Console.WriteLine("Writing to file");
    }

    public void Search(string text)
    {
        Console.WriteLine("Searching in file");
    }
}

public class Program
{
    public static void Main()
    {
        IFile file1 = new FileInfo();
        FileInfo file2 = new FileInfo();
		
        file1.ReadFile(); 
        file1.WriteFile("content"); 
        //file1.Search("text to be searched")//compile-time error 
        
        file2.Search("text to be searched");
        //file2.ReadFile(); //compile-time error 
        //file2.WriteFile("content"); //compile-time error 
    }
}
尝试一下

在上面的示例中,file1 对象只能访问 IFile 的成员,而 file2 只能访问 FileInfo 类的成员。这是显式实现的限制。

实现多个接口

类或结构可以实现多个接口。它必须提供所有接口所有成员的实现。

示例:实现多个接口
interface IFile
{
    void ReadFile();
}

interface IBinaryFile
{
    void OpenBinaryFile();
    void ReadFile();
}

class FileInfo : IFile, IBinaryFile
{
    void IFile.ReadFile()
    {
        Console.WriteLine("Reading Text File");
    }

    void IBinaryFile.OpenBinaryFile()
    {
        Console.WriteLine("Opening Binary File");
    }

    void IBinaryFile.ReadFile()
    {
        Console.WriteLine("Reading Binary File");
    }

    public void Search(string text)
    {
        Console.WriteLine("Searching in File");
    }
}

public class Program
{
    public static void Main()
    {
        IFile file1 = new FileInfo();
        IBinaryFile file2 = new FileInfo();
        FileInfo file3 = new FileInfo();
		
        file1.ReadFile(); 
        //file1.OpenBinaryFile(); //compile-time error 
        //file1.SearchFile("text to be searched"); //compile-time error 
        
        file2.OpenBinaryFile();
        file2.ReadFile();
        //file2.SearchFile("text to be searched"); //compile-time error 
    
        file3.Search("text to be searched");
        //file3.ReadFile(); //compile-time error 
        //file3.OpenBinaryFile(); //compile-time error 
    }
}
尝试一下

上面,FileInfo 显式实现了两个接口 IFile 和 IBinaryFile。在实现多个接口时,建议显式实现接口,以避免混淆并提高可读性。

默认接口方法

到目前为止,我们了解到接口只能包含方法声明。C# 8.0 增加了对接口中带有具体实现的虚拟扩展方法的支持。

虚拟接口方法也称为默认接口方法,不需要在类或结构中实现。

示例:默认接口方法
interface IFile
{
    void ReadFile();
    void WriteFile(string text);

    void DisplayName()
    {
        Console.WriteLine("IFile");
    }
}

在上面的 IFile 接口中,DisplayName() 是默认方法。对于所有实现 IFile 接口的类,实现将保持不变。请注意,类不从其接口继承默认方法;因此,不能使用类实例访问它。

示例:接口实现
class FileInfo : IFile
{
    public void ReadFile()
    {
        Console.WriteLine("Reading File");
    }

    public void WriteFile(string text)
    {
        Console.WriteLine("Writing to file");
    }
}

public class Program
{
    public static void Main()
    {
        IFile file1 = new FileInfo();
        file1.ReadFile(); 
        file1.WriteFile("content"); 
        file1.DisplayName();

        FileInfo file2 = new FileInfo();
        //file2.DisplayName(); //compile-time error 
    }
}
尝试一下

了解更多关于默认接口方法的信息。

接口中的修饰符

C# 8.0 允许在接口中使用 private、protected、internal、public、virtual、abstract、sealed、static、extern 和 partial 修饰符。

  • 所有接口成员的默认访问级别为 public。
  • 包含方法体的接口成员是 virtual 成员,除非使用了 sealed 或 private 修饰符。
  • 接口的 private 或 sealed 函数成员必须具有实现体。
  • 接口可以声明静态成员,可以通过接口名称访问。

了解更多关于接口中修饰符的信息。

TUTORIALSTEACHER.COM

TutorialsTeacher.com 是您权威的技术教程来源,旨在通过循序渐进的方法,指导您掌握各种网络和其他技术。

我们的内容旨在帮助所有级别的学习者轻松快速地学习技术。访问此平台即表示您已阅读并同意遵守我们的使用条款和隐私政策,这些条款和政策旨在保护您的体验和隐私权。

[email protected]

关于我们使用条款隐私政策
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) 版权所有。