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)会有不同的结果。
      接口也可以实现多态啊!


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

  • 相关阅读:
    Beyond Compare保存快照和CRC比较相结合的方法
    如何在Beyond Compare文本比较时设置书签
    如何使用Navicat for SQLite 触发器
    Navicat Premium 中实用工具介绍
    Beyond Compare查看合并文本后相同内容的方法
    Marriage Match II HDU
    Escape HDU
    kebab HDU
    Task Schedule HDU
    网络流深入
  • 原文地址:https://www.cnblogs.com/wzg0319/p/1673414.html
Copyright © 2011-2022 走看看