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

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

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

本示例聲明一個接口IDimensions 和一個類 Box,顯式實(shí)現(xiàn)了接口成員 GetLength GetWidth。 通過接口實(shí)例 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 方法中以下行,因?yàn)樗鼈儗a(chǎn)生編譯錯誤。 顯式實(shí)現(xiàn)的接口成員不能從類實(shí)例訪問:
//System.Console.WriteLine("Length: {0}", box1.GetLength());
//System.Console.WriteLine("Width: {0}", box1.GetWidth());
  • 另請注意 Main 方法中的以下行成功輸出了框的尺寸,因?yàn)檫@些方法是從接口實(shí)例調(diào)用的:
System.Console.WriteLine("Length: {0}", dimensions.GetLength());
System.Console.WriteLine("Width: {0}", dimensions.GetWidth());

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

相關(guān)文章

最新評論

太和县| 宁明县| 汾西县| 武胜县| 土默特右旗| 静乐县| 华安县| 无为县| 金塔县| 西林县| 玛纳斯县| 奇台县| 嘉黎县| 县级市| 额济纳旗| 南木林县| 紫阳县| 满城县| 南丹县| 屏东市| 西盟| 平原县| 新田县| 共和县| 阳春市| 芦山县| 新邵县| 凤山县| 茂名市| 靖安县| 汽车| 宁都县| 玛多县| 潮州市| 清新县| 法库县| 西和县| 郁南县| 铜鼓县| 象山县| 武冈市|