设为首页
联系站长
加入收藏

您的位置: 首页>>IT业界>>互联网>>正文
 C# 程序员参考--显式接口实现教程
 日期:2005-6-3 10:51:10  来源:100down.com

本教程演示如何显式实现接口成员以及如何从接口实例访问这些成员。

教程

实现接口的类可以显式实现该接口的成员。当显式实现某成员时,不能通过类实例访问该成员,而只能通过该接口的实例访问该成员。本教程包含两个示例。第一个示例阐释如何显式实现和访问接口成员。第二个示例展示如何实现具有相同成员名的两个接口。

示例 1

本示例声明一个 IDimensions 接口和一个 Box 类,该类显式实现接口成员 LengthWidth。通过接口实例 myDimensions 访问这些成员。

// explicit1.cs
interface IDimensions 
{
   float Length();
   float Width();
}

class Box : IDimensions 
{
   float lengthInches;
   float widthInches;

   public Box(float length, float width) 
   {
      lengthInches = length;
      widthInches = width;
   }
   // Explicit interface member implementation: 
   float IDimensions.Length() 
   {
      return lengthInches;
   }
   // Explicit interface member implementation:
   float IDimensions.Width() 
   {
      return widthInches;      
   }

   public static void Main() 
   {
      // Declare a class instance "myBox":
      Box myBox = new Box(30.0f, 20.0f);
      // Declare an interface instance "myDimensions":
      IDimensions myDimensions = (IDimensions) myBox;
      // Print out the dimensions of the box:
      /* 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}", myBox.Length());
      //System.Console.WriteLine("Width: {0}", myBox.Width());
      /* Print out the dimensions of the box by calling the methods 
         from an instance of the interface:                         */
      System.Console.WriteLine("Length: {0}", myDimensions.Length());
      System.Console.WriteLine("Width: {0}", myDimensions.Width());
   }
}

输出

Length: 30
Width: 20

代码讨论

  • 请注意 Main 方法中下列代码行被注释掉,因为它们将产生编译错误。显式实现的接口成员不能从类实例访问:
    //System.Console.WriteLine("Length: {0}", myBox.Length());
    //System.Console.WriteLine("Width: {0}", myBox.Width());
  • 还请注意,Main 方法中的下列代码行成功输出框的尺寸,因为这些方法是从接口实例调用的:
    System.Console.WriteLine("Length: {0}", myDimensions.Length());
    System.Console.WriteLine("Width: {0}", myDimensions.Width());

示例 2

显式接口实现还允许程序员继承共享相同成员名的两个接口,并为每个接口成员提供一个单独的实现。本示例同时以公制单位和英制单位显示框的尺寸。Box 类继承 IEnglishDimensionsIMetricDimensions 两个接口,它们表示不同的度量衡系统。两个接口有相同的成员名 LengthWidth

// explicit2.cs
// Declare the English units interface:
interface IEnglishDimensions 
{
   float Length();
   float Width();
}
// Declare the metric units interface:
interface IMetricDimensions 
{
   float Length();
   float Width();
}
// Declare the "Box" class that implements the two interfaces:
// IEnglishDimensions and IMetricDimensions:
class Box : IEnglishDimensions, IMetricDimensions 
{
   float lengthInches;
   float widthInches;
   public Box(float length, float width) 
   {
      lengthInches = length;
      widthInches = width;
   }
// Explicitly implement the members of IEnglishDimensions:
   float IEnglishDimensions.Length() 
   {
      return lengthInches;
   }
   float IEnglishDimensions.Width() 
   {
      return widthInches;      
   }
// Explicitly implement the members of IMetricDimensions:
   float IMetricDimensions.Length() 
   {
      return lengthInches * 2.54f;
   }
   float IMetricDimensions.Width() 
   {
      return widthInches * 2.54f;
   }
   public static void Main() 
   {
      // Declare a class instance "myBox":
      Box myBox = new Box(30.0f, 20.0f);
      // Declare an instance of the English units interface:
      IEnglishDimensions eDimensions = (IEnglishDimensions) myBox;
      // Declare an instance of the metric units interface:
      IMetricDimensions mDimensions = (IMetricDimensions) myBox;
      // Print dimensions in English units:
      System.Console.WriteLine("Length(in): {0}", eDimensions.Length());
      System.Console.WriteLine("Width (in): {0}", eDimensions.Width());
      // Print dimensions in metric units:
      System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
      System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
   }
}

输出

Length(in): 30
Width (in): 20
Length(cm): 76.2
Width (cm): 50.8

代码讨论

如果希望默认度量采用英制单位,请正常实现 LengthWidth 这两个方法,并从 IMetricDimensions 接口显式实现 Length 和 Width 方法:

// Normal implementation:
public float Length()
{
   return lengthInches;
}
public float Width()
{
   return widthInches;
}
// Explicit implementation:
float IMetricDimensions.Length() 
{
   return lengthInches * 2.54f;
}
float IMetricDimensions.Width() 
{
   return widthInches * 2.54f;
}

这种情况下,可以从类实例访问英制单位,而从接口实例访问公制单位:

System.Console.WriteLine("Length(in): {0}", myBox.Length());
System.Console.WriteLine("Width (in): {0}", myBox.Width());   
System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
相关文章

·C# 语言规范:自动内存管理
·C# 语言规范:变量和参数
·C# 语言规范: 类型
·C# 语言规范 开始
·C# 程序员 OLE DB 教程
·C# 不安全代码教程
·C# 线程处理教程
·C# 程序员安全性教程
·C# 程序员参考--属性教程
·C# 服务器教程


阅读排行

·Win 2000不能访问Win XP的原因及
·解析Windows中的帐户和权限功能
·如何共享Windows XP操作系统
·Windows XP Service Pack 1
·通过 Windows XP 注册表自定义您
·Windows 2000 TCP/IP协议概述
·Windows 2000 Server TCP/IP协议
·dos常用命令使用说明
·Windows变慢原因分析及解决方法(
·Windows变慢原因分析及解决方法(
·WINXP下强行关闭“杀”不了的进程
·Windows XP系统注册表的恢复

最新文档

·创业激情需冷静 网络代理选择应理
·域名交易平台亟需规范 预防域名买
·Travel旅游域名即将引发抢注潮 
·从原理深处分析如何预防arp攻击
·突破建站弊端 企业网络营销大有可
·如何区分进程和病毒?
·java的基础知识,如何学好java
·微机原理与接口技术基础知识
·如何解决青少年沉迷网络的问题,
·全面分析主板BIOS报警信号

请您注意:
·尊重网上道德,遵守中华人民共和国的各项有关法律法规
·您在本站发表的作品,本站有权在网站内转载或引用
·其他网站如果需要转载 本站文章请在贵站著名来源,谢谢合作