程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 程序員參考--顯式接口完成教學文章

C# 程序員參考--顯式接口完成教學文章

編輯:C#入門知識

本教程演示如何顯式實現接口成員以及如何從接口實例訪問這些成員。

教程

實現接口的類可以顯式實現該接口的成員。當顯式實現某成員時,不能通過類實例訪問該成員,而只能通過該接口的實例訪問該成員。本教程包含兩個示例。第一個示例闡釋如何顯式實現和訪問接口成員。第二個示例展示如何實現具有相同成員名的兩個接口。

示例 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 me

[1] [2] 下一頁  

mbers 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());

 

上一頁  [1] [2] 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved