跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
代码酷
搜索
搜索
中文(中国大陆)
外观
创建账号
登录
个人工具
创建账号
登录
未登录编辑者的页面
了解详情
贡献
讨论
编辑“︁
C Sharp 接口
”︁(章节)
页面
讨论
大陆简体
阅读
编辑
编辑源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
编辑
编辑源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
您的更改会在有权核准的用户核准后向读者展示。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
= C#接口 = == 介绍 == 在C#中,'''接口(Interface)'''是一种引用类型,它定义了一组相关的功能(方法、属性、事件或索引器)的契约,但不提供具体实现。接口允许开发者实现多态行为,并促进代码的解耦和可扩展性。类或结构体可以通过实现接口来承诺提供接口中定义的所有成员。 接口的主要特点包括: * 不能直接实例化 * 不包含字段(C# 8.0之前) * 成员默认是公共的且抽象的 * 支持多重继承(一个类可以实现多个接口) == 基本语法 == 接口使用<code>interface</code>关键字定义: <syntaxhighlight lang="csharp"> public interface IExampleInterface { void DoSomething(); int Calculate(int a, int b); string Name { get; set; } } </syntaxhighlight> 实现接口的类必须提供所有成员的具体实现: <syntaxhighlight lang="csharp"> public class ExampleClass : IExampleInterface { public string Name { get; set; } public void DoSomething() { Console.WriteLine("Doing something..."); } public int Calculate(int a, int b) { return a + b; } } </syntaxhighlight> == 接口特性详解 == === 显式接口实现 === 当类实现多个含有相同成员名的接口时,可以使用显式实现来消除歧义: <syntaxhighlight lang="csharp"> interface IFirst { void Method(); } interface ISecond { void Method(); } class Implementation : IFirst, ISecond { void IFirst.Method() => Console.WriteLine("IFirst implementation"); void ISecond.Method() => Console.WriteLine("ISecond implementation"); } </syntaxhighlight> 调用方式: <syntaxhighlight lang="csharp"> var obj = new Implementation(); ((IFirst)obj).Method(); // 输出: IFirst implementation ((ISecond)obj).Method(); // 输出: ISecond implementation </syntaxhighlight> === 默认接口方法(C# 8.0+) === C# 8.0引入了接口方法的默认实现: <syntaxhighlight lang="csharp"> public interface ILogger { void Log(string message); // 默认实现 void LogError(string error) => Log($"ERROR: {error}"); } </syntaxhighlight> === 接口继承 === 接口可以继承其他接口: <syntaxhighlight lang="csharp"> public interface IReadable { string Read(); } public interface IWritable : IReadable { void Write(string content); } </syntaxhighlight> 类实现<code>IWritable</code>时必须同时实现<code>IReadable</code>的成员。 == 实际应用案例 == === 插件系统 === 接口常用于实现插件架构: <syntaxhighlight lang="csharp"> public interface IPlugin { string Name { get; } void Execute(); } public class CalculatorPlugin : IPlugin { public string Name => "Calculator Plugin"; public void Execute() { Console.WriteLine("Calculator plugin is running..."); // 具体计算逻辑 } } </syntaxhighlight> === 依赖注入 === 现代框架如ASP.NET Core广泛使用接口进行依赖注入: <syntaxhighlight lang="csharp"> public interface IEmailService { void SendEmail(string to, string subject, string body); } public class SmtpEmailService : IEmailService { public void SendEmail(string to, string subject, string body) { // 使用SMTP发送邮件 } } // 在控制器中使用 public class UserController { private readonly IEmailService _emailService; public UserController(IEmailService emailService) { _emailService = emailService; } } </syntaxhighlight> == 接口 vs 抽象类 == <mermaid> classDiagram class 接口 { <<interface>> 只能包含抽象成员(默认) 支持多重继承 不能包含字段(C#8.0前) 不能有构造函数 } class 抽象类 { 可以包含具体实现 单继承 可以包含字段 可以有构造函数 } </mermaid> 主要区别: * 抽象类可以包含实现,接口不能(C# 8.0前) * 类只能继承一个抽象类,但可以实现多个接口 * 抽象类可以包含字段,接口不能(C# 8.0前) * 抽象类可以有构造函数,接口不能 == 最佳实践 == 1. 使用接口定义'''角色'''而不是'''类型''' 2. 遵循接口隔离原则(ISP) - 保持接口小巧专注 3. 使用<code>I</code>前缀命名接口(如<code>IEnumerable</code>) 4. 考虑使用接口进行单元测试的模拟 5. 在需要跨组件边界通信时优先使用接口 == 高级主题 == === 协变和逆变接口 === C#支持泛型接口中的变体: <syntaxhighlight lang="csharp"> // 协变(out) interface IEnumerable<out T> { /*...*/ } // 逆变(in) interface IComparer<in T> { /*...*/ } </syntaxhighlight> 数学表示: * 协变:若<math>A \leq B</math>,则<math>I<A> \leq I<B></math> * 逆变:若<math>A \leq B</math>,则<math>I<B> \leq I<A></math> === 接口重新实现 === 派生类可以重新实现接口: <syntaxhighlight lang="csharp"> interface IBase { void Method(); } class Base : IBase { public void Method() => Console.WriteLine("Base"); } class Derived : Base, IBase { public new void Method() => Console.WriteLine("Derived"); } </syntaxhighlight> == 总结 == C#接口是面向对象设计中的强大工具,它: * 促进松耦合设计 * 实现多态行为 * 支持多重继承 * 提高代码可测试性 * 是现代框架(如.NET Core)的基础 通过合理使用接口,可以创建更灵活、可维护和可扩展的应用程序架构。 [[Category:编程语言]] [[Category:C Sharp]] [[Category:C Sharp 高级类特性]]
摘要:
请注意,所有对代码酷的贡献均被视为依照知识共享署名-非商业性使用-相同方式共享发表(详情请见
代码酷:著作权
)。如果您不希望您的文字作品被随意编辑和分发传播,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)