服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - c#如何显式实现接口成员

c#如何显式实现接口成员

2022-10-11 14:48olprod C#

这篇文章主要介绍了c#如何显式实现接口成员,帮助大家更好的利用c#处理接口,感兴趣的朋友可以了解下

本示例声明一个接口IDimensions 和一个类 Box,显式实现了接口成员 GetLength GetWidth。 通过接口实例 dimensions 访问这些成员。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
interface IDimensions
{
  float GetLength();
  float GetWidth();
}
 
class Box : IDimensions
{
  float lengthInches;
  float widthInches;
 
  Box(float length, float width)
  {
    lengthInches = length;
    widthInches = width;
  }
  // Explicit interface member implementation:
  float IDimensions.GetLength()
  {
    return lengthInches;
  }
  // Explicit interface member implementation:
  float IDimensions.GetWidth()
  {
    return widthInches;
  }
 
  static void Main()
  {
    // Declare a class instance box1:
    Box box1 = new Box(30.0f, 20.0f);
 
    // Declare an interface instance dimensions:
    IDimensions dimensions = box1;
 
    // The following commented lines would produce compilation
    // errors because they try to access an explicitly implemented
    // interface member from a class instance:
    //System.Console.WriteLine("Length: {0}", box1.GetLength());
    //System.Console.WriteLine("Width: {0}", box1.GetWidth());
 
    // Print out the dimensions of the box by calling the methods
    // from an instance of the interface:
    System.Console.WriteLine("Length: {0}", dimensions.GetLength());
    System.Console.WriteLine("Width: {0}", dimensions.GetWidth());
  }
}
/* Output:
  Length: 30
  Width: 20
*/

可靠编程

  • 请注意,注释掉了 Main 方法中以下行,因为它们将产生编译错误。 显式实现的接口成员不能从类实例访问:
?
1
2
//System.Console.WriteLine("Length: {0}", box1.GetLength());
//System.Console.WriteLine("Width: {0}", box1.GetWidth());
  • 另请注意 Main 方法中的以下行成功输出了框的尺寸,因为这些方法是从接口实例调用的:
?
1
2
System.Console.WriteLine("Length: {0}", dimensions.GetLength());
System.Console.WriteLine("Width: {0}", dimensions.GetWidth());

以上就是c#如何显式实现接口成员的详细内容,更多关于c# 显式实现接口成员的资料请关注服务器之家其它相关文章!

原文链接:https://github.com/dotnet/docs.zh-cn/blob/live/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-interface-members.md

延伸 · 阅读

精彩推荐
  • C#C# xmlSerializer简单用法示例

    C# xmlSerializer简单用法示例

    这篇文章主要介绍了C# xmlSerializer简单用法,结合实例形式分析了C#基于xmlSerializer操作xml的读取、输出等相关操作技巧,需要的朋友可以参考下...

    peerlessbloom11492022-01-19
  • C#C# 获取当前年份的周期及周期所在日期范围(推荐)

    C# 获取当前年份的周期及周期所在日期范围(推荐)

    这篇文章主要介绍了C# 获取当前年份的周期,周期所在日期范围 ,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下...

    有创软件工作室5652022-02-23
  • C#C# 静态构造函数使用总结

    C# 静态构造函数使用总结

    今天花了一些时间把静态构造函数的用法总结了一下,希望高手们指点。谢谢...

    C#菜鸟教程2122020-12-19
  • C#Unity实现引导页效果

    Unity实现引导页效果

    这篇文章主要为大家详细介绍了Unity实现引导页效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    贪玩的孩纸时代11322022-09-02
  • C#C#使用String和StringBuilder运行速度测试及各自常用方法简介

    C#使用String和StringBuilder运行速度测试及各自常用方法简介

    今天小编就为大家分享一篇关于C#使用String和StringBuilder运行速度测试及各自常用方法简介,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值...

    Czhenya8642022-03-03
  • C#让C# Excel导入导出 支持不同版本Office

    让C# Excel导入导出 支持不同版本Office

    让C# Excel导入导出,支持不同版本的Office,感兴趣的小伙伴们可以参考一下...

    ichk3952021-12-06
  • C#C#无损高质量压缩图片代码

    C#无损高质量压缩图片代码

    这篇文章主要为大家详细介绍了C#无损高质量压缩图片代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    边界流浪者9402022-01-05
  • C#C#学习笔记整理_浅谈Math类的方法

    C#学习笔记整理_浅谈Math类的方法

    下面小编就为大家带来一篇C#学习笔记整理_浅谈Math类的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看...

    C#教程网5832021-12-07