• 接口的实现


    接口的实现分为隐式实现,显式实现和含有显式和隐式实现得到实现方式,下面将详细讲解这三种实现方式

    一.隐式实现

    interface MyInterface
     {
       void ImpMean();
     }
     public class ImpClass:MyInterface
    {
       public void ImpMean()
         {
           Console.WriteLine("接口的隐式实现");
         }


     } 

    class Program

     {
       static void Main(string[] args)
         {
          ImpClass impclass = new ImpClass();
           impclass.ImpMean();
           ((MyInterface)impclass).ImpMean();
           Console.ReadKey();
         }
    }

    控制台的最终显示为:

    接口的隐式实现

    接口的隐式实现

    二.显式实现

    interface Myterface
      {
         void Paint();
      }
    public class Emplicit:Myterface
      {
         void Myterface.Paint()
         {
           Console.WriteLine("接口的显式实现");
         }
      }
    class Program
     {
       static void Main(string[] args)
       {
           Emplicit emplicit = new Emplicit();
           ((Myterface)emplicit).Paint();
           Console.ReadKey();
       }
     }

    控制台的最终显示为:

    接口的显式实现

    三.同时含有显式和隐式实现

      interface MyInterface
        {
          void Write();
        }

      public class Synthesize : MyInterface
        {
          public void Write()
            {
              Console.WriteLine("接口的综合实现之一");
            }

          void MyInterface.Write()
            {
              Console.WriteLine("接口的综合实现之二");
            }

        }
      class Program
        {
          static void Main(string[] args)
          {
            Synthesize synthesize = new Synthesize();
            synthesize.Write();
            ((MyInterface)synthesize).Write();
            Console.ReadKey();
          }
        }

    控制台的最终显示为:

    接口的综合实现之一

    接口的综合实现之二

    注意:当同时有显式和隐式的实现时,显式实现才是真正的实现方法

  • 相关阅读:
    测试开发题目整理(二)
    测试开发题目整理(一)
    Python + request接口测试中Cookie和Session的获取和使用
    requests发送HTTPS请求(处理SSL证书验证)
    js ES5面向对象继承 模仿ES6
    如何使用canvas绘制椭圆,扩展非chrome浏览器中的ellipse方法
    javascript对象创建及继承
    观察者模式及事件与委托的区别-20171215内训会
    html5悬浮球效果
    文本框高度自适应不出滚动条
  • 原文地址:https://www.cnblogs.com/SancoLee/p/4575676.html
Copyright © 2020-2023  润新知