最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#如何顯式實現(xiàn)接口成員

 更新時間:2020年10月14日 08:35:41   作者:olprod  
這篇文章主要介紹了c#如何顯式實現(xiàn)接口成員,幫助大家更好的利用c#處理接口,感興趣的朋友可以了解下

本示例聲明一個接口IDimensions 和一個類 Box,顯式實現(xiàn)了接口成員 GetLength GetWidth。 通過接口實例 dimensions 訪問這些成員。

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 方法中以下行,因為它們將產(chǎn)生編譯錯誤。 顯式實現(xiàn)的接口成員不能從類實例訪問:
//System.Console.WriteLine("Length: {0}", box1.GetLength());
//System.Console.WriteLine("Width: {0}", box1.GetWidth());
  • 另請注意 Main 方法中的以下行成功輸出了框的尺寸,因為這些方法是從接口實例調(diào)用的:
System.Console.WriteLine("Length: {0}", dimensions.GetLength());
System.Console.WriteLine("Width: {0}", dimensions.GetWidth());

以上就是c#如何顯式實現(xiàn)接口成員的詳細內(nèi)容,更多關(guān)于c# 顯式實現(xiàn)接口成員的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

崇义县| 青铜峡市| 琼海市| 库伦旗| 罗山县| 丹凤县| 靖边县| 沙田区| 宁明县| 清新县| 安多县| 治县。| 广丰县| 车险| 南部县| 卢湾区| 攀枝花市| 蒙阴县| 金平| 临城县| 连平县| 平原县| 红桥区| 织金县| 富蕴县| 青田县| 边坝县| 麻阳| 台东县| 慈溪市| 凌源市| 淮安市| 湛江市| 内黄县| 含山县| 阜新市| 镇安县| 新津县| 济南市| 阿拉善右旗| 安徽省|