zoukankan      html  css  js  c++  java
  • 接口的实便

    接口就是一个先签协议(接口定义),后实现。  
      本示例显示声明一个   IDimensions   接口和一个   Box   类,该类显式实现接口成员   Length   和   Width。通过接口实例   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  

    实例二:

    interface   Itest  
      {  
          string   ShowText();  
      }  
       
      class   class1:Itest  
      {  
          class1(){}  
          string   ShowText()  
          {  
              return   "Class1   Show   Text!";  
          }  
       
      }  
       
      class   class2:Itest  
      {  
          class2(){}  
          string   ShowText()  
          {  
              return   "Class2   Show   Text!";  
          }  
       
      }  
       
      可以写个函数:  
      void   ShowTextTest(Itest   itest)  
      {  
          string   temp;  
          temp=itest.ShowText();  
      }   
          分别把class1和class2的实例传给ShowTextTest(cls1)会有不同的结果。
      接口也可以实现多态啊!


       本人博客的文章大部分来自网络转载,因为时间的关系,没有写明转载出处和作者。所以在些郑重的说明:文章只限交流,版权归作者。谢谢

  • 相关阅读:
    sharpen和filter2D
    Changing the contrast and brightness of an image!(opencv对比度和亮度调节)
    人脸表情识别
    Pycharm下载和安装
    Anaconda下载与安装
    图像人脸检测+人眼检测 (opencv + c++)
    cv2.VideoWriter()指定写入视频帧编码格式
    python_openCV例程遇到error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale的简单解决方法
    图像处理库 Pillow与PIL
    IDE bulid构建隐藏了什么(预处理->编译->汇编->链接)
  • 原文地址:https://www.cnblogs.com/wzg0319/p/1673414.html
Copyright © 2011-2022 走看看