码迷,mamicode.com
首页 > 其他好文 > 详细

接口的实现

时间:2015-06-14 21:21:04      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

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

一.隐式实现

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();
      }
    }

控制台的最终显示为:

接口的综合实现之一

接口的综合实现之二

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

接口的实现

标签:

原文地址:http://www.cnblogs.com/SancoLee/p/4575676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!