接口的实现

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

一.隐式实现

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

控制台的最终显示为:

接口的综合实现之一

接口的综合实现之二

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

posted @ 2015-06-14 20:41  Sancos  阅读(121)  评论(0编辑  收藏  举报