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

笔记:显示实现接口如何实现,作用是什么

一、目的:简要介绍显示实现接口的如何实现,作用是什么

        显式实现接口是指在类中实现接口的方法或属性时,必须在方法或属性的声明中显式地指定接口名称。显式实现的接口成员只能通过接口引用访问,而不能通过类的实例直接访问。 


二、实现

1. 定义接口


首先,定义一个接口,其中包含一些方法或属性。

public interface IExample
{void DoSomething();string GetInfo();
}

2. 显式实现接口


在类中实现接口的方法和属性时,必须在方法或属性的声明中显式地指定接口名称。 

public class ExampleClass : IExample
{void IExample.DoSomething(){Console.WriteLine("Doing something...");}string IExample.GetInfo(){return "Example information";}
}

使用示例 

class Program
{static void Main(string[] args){IExample example = new ExampleClass();example.DoSomething(); // 通过接口引用访问方法Console.WriteLine(example.GetInfo());}
}

在这个示例中,ExampleClass 显式实现了 IExample 接口的方法和属性。要调用这些方法和属性,必须将 ExampleClass 的实例转换为 IExample 接口类型。


三、作用


1. 避免命名冲突


显式实现接口可以避免命名冲突。如果类实现了多个接口,并且这些接口中有同名的方法或属性,显式实现可以确保每个接口的方法或属性都能正确实现。
示例: 

public interface IFirst
{void DoSomething();
}public interface ISecond
{void DoSomething();
}public class ExampleClass : IFirst, ISecond
{void IFirst.DoSomething(){Console.WriteLine("First interface implementation");}void ISecond.DoSomething(){Console.WriteLine("Second interface implementation");}
}class Program
{static void Main(string[] args){IFirst first = new ExampleClass();ISecond second = new ExampleClass();first.DoSomething(); // 输出: First interface implementationsecond.DoSomething(); // 输出: Second interface implementation}
}

 2. 控制接口成员的可见性


显式实现的接口成员只能通过接口引用访问,而不能通过类的实例直接访问。这可以提高封装性,防止接口成员被意外调用。
示例:

public interface IExample
{void DoSomething();
}public class ExampleClass : IExample
{void IExample.DoSomething(){Console.WriteLine("Doing something...");}public void AnotherMethod(){Console.WriteLine("Another method");}
}class Program
{static void Main(string[] args){ExampleClass example = new ExampleClass();// example.DoSomething(); // 编译错误: 'ExampleClass' does not contain a definition for 'DoSomething'example.AnotherMethod(); // 输出: Another methodIExample iExample = example;iExample.DoSomething(); // 输出: Doing something...}
}

总结 

显式实现接口用于避免命名冲突和控制接口成员的可见性。显式实现的接口成员只能通过接口引用访问,而不能通过类的实例直接访问。这种实现方式在处理多个接口的同名成员或希望提高封装性时非常有用。 

需要了解的知识点 

显式接口实现 - C# | Microsoft Learn 

如何显式实现接口成员 - C# | Microsoft Learn 

如何显式实现两个接口的成员 - C# | Microsoft Learn 

了解更多 

System.Windows.Controls 命名空间 | Microsoft Learn

控件库 - WPF .NET Framework | Microsoft Learn

WPF 介绍 | Microsoft Learn

XAML概述 - WPF .NET | Microsoft Learn

Windows Presentation Foundation 简介 - WPF .NET | Microsoft Learn

使用 Visual Studio 创建新应用教程 - WPF .NET | Microsoft Learn

适用于 .NET 8 的 WPF 的新增功能 - WPF .NET | Microsoft Learn

适用于 .NET 7 的 WPF 的新增功能 - WPF .NET | Microsoft Learn

System.Windows.Controls 命名空间 | Microsoft Learn

Reference Source

Sysinternals - Sysinternals | Microsoft Learn

Windows app development documentation - Windows apps | Microsoft Learn

欢迎使用 Expression Blend | Microsoft Learn

https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/?view=netdesktop-7.0&WT.mc_id=MVP_380318

https://github.com/HeBianGu

HeBianGu的个人空间-HeBianGu个人主页-哔哩哔哩视频

http://www.xdnf.cn/news/7651.html

相关文章:

  • ollama部署模型
  • 工单派单应用:5 大核心功能提升协作效率
  • Ai学习之LangChain框架
  • STM32外设应用详解——从基础到高级应用的全面指南
  • 差分数组:原理与应用
  • 文献分享-临床预测模型-基于围手术期时间数据肝切除术后肝衰竭早期检测
  • CSS 背景全解析:从基础属性到视觉魔法
  • MinIO集群故障,其中一块driver-4异常
  • 网络安全之带正常数字签名的后门样本分析
  • 软件测试之环境搭建及测试流程
  • 见多识广10:大模型的一些基础概念
  • Python训练营打卡——DAY31(2025.5.20)
  • 类和对象------2
  • Leetcode百题斩-字典树
  • MySQL 安全更新大量数据
  • MySQL高可用之ProxySQL + MGR 实现读写分离实战
  • 面向AI研究的模块化即插即用架构综述与资源整理全覆盖
  • 数据库实验——备份与恢复
  • 【普及−】洛谷P1862 ——输油管道问题
  • 【latex】文本颜色修改
  • 【QT】QTableWidget获取width为100,与真实值不符问题解决
  • C++ 网络编程(9)字节序处理和消息队列的控制
  • 缺乏进度跟踪机制,如何掌握项目状态?
  • MyBatis常用方法
  • 零售EDI:Belk Stores EDI需求分析
  • 阅读笔记---城市计算中用于预测学习的时空图神经网络研究综述
  • 《从零开始构建高可用MySQL架构:全流程实战指南》
  • 无人机避障——深蓝学院浙大Fast-planner学习部分(轨迹生成B-Spline部分)
  • Spring是如何实现scope作用域支持
  • 家用和类似用途电器的安全 第1部分:通用要求 与2005版差异(6)