C#显式接口实现用法详细介绍

2021年5月9日16:37:15 发表评论 726 次浏览

接口是具有共同功能或属性的松散项目的集合。接口包含方法签名, 属性, 事件等。使用接口是为了使一个类或结构可以实现多种行为。 C#不支持

多重继承

因为它造成的歧义。但是, 许多现实生活中的对象不仅继承一种类型的属性, 因此使用接口代替扩展类。

接口仅由签名组成, 而不由其实现组成, 因此, 实现接口的任何类或结构都必须通过重写来提供实现。

什么是显式接口实现?何时使用?

明确告诉编译器某个成员属于该特定接口, 这称为显式接口实现。

如果一个类从具有一个具有相同签名的方法的多个接口实现, 则对该方法的调用将实现相同的方法, 而不是接口特定的方法。这将破坏使用不同接口的全部目的。这就是显式实现的时候。使用显式实现, 你可以告诉编译器你正在重载哪个接口的方法, 并且可以为不同接口的方法提供不同的功能。对于其他任何类型的成员(如属性, 事件), 情况也是如此。

语法如下:

class ClassName : InterfaceName
{
    returnType InterfaceName.method()
    { 
          //Your Code 
    }
}

示例1:该程序显示了显式接口实现的使用。这里我们有两个接口I1和I2具有相同的方法签名, 称为printMethod, 返回类型为void。类C实现这两个接口, 因此我们使用显式接口实现来区分方法。

//C# Program to show the use of
//Explicit interface implementation
using System;
  
interface I1 {
  
     void printMethod();
}
  
interface I2 {
  
     void printMethod();
}
  
//class C implements two interfaces
class C : I1, I2 {
  
     //Explicitly implements method of I1
     void I1.printMethod()
     {
         Console.WriteLine( "I1 printMethod" );
     }
  
     //Explicitly implements method of I2
     void I2.printMethod()
     {
         Console.WriteLine( "I2 printMethod" );
     }
}
  
//Driver Class
class GFG {
  
     //Main Method
     static void Main( string [] args)
     {
         I1 i1 = new C();
         I2 i2 = new C();
  
         //call to method of I1
         i1.printMethod();
  
         //call to method of I2
         i2.printMethod();
     }
}

输出如下:

I1 printMethod
I2 printMethod

示例2:在这里, 我们有一个方法的接口金字塔drawPyramid。班级显示继承此方法, 并提供了一个打印"*在屏幕上的金字塔。我们使用Explicit实现重写drawPyramid方法。

//C# Program to show the use of
//Explicit interface implementation
using System;
  
interface Pyramid {
  
     //Method signature
     void drawPyramid();
}
  
//Implements Pyramid
class Display : Pyramid {
  
     //Using Explicit implementation
     void Pyramid.drawPyramid()
     {
         for ( int i = 1; i <= 10; i++) 
         {
             for ( int k = i; k <10; k++)
                 Console.Write( " " );
  
             for ( int j = 1; j <= (2 * i - 1); j++)
                 Console.Write( "*" );
  
             Console.WriteLine();
         }
     }
}
  
//Driver Code
class GFG {
  
     //Main Method
     static void Main( string [] args)
     {
         //Create object of the class using Interface
         Pyramid obj = new Display();
  
         //call method
         obj.drawPyramid();
     }
}

输出如下:

*
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************

示例3:我们可以将显式接口实现用于甚至属性以及基本上接口的其他任何成员。在这里, 我们有财产X和方法X在界面中I1和I2分别具有相同的名称和返回类型。我们仅对方法使用显式接口实现X。这样, 编译器将使用该属性X除非另有说明。

//C# Program to show the use of
//Explicit interface implementation
using System;
  
interface I1 {
  
     //Property X
     int X
     {
         set ;
         get ;
     }
}
  
interface I2 {
  
     //Method X
     int X();
}
  
class C : I1, I2 {
  
     int x;
  
     //Implicit implementation of
     //the property
     public int X
     {
         set { x = value; }
         get { return x; }
     }
  
     //Explicit implementation of
     //the method
     int I2.X()
     {
         return 0;
     }
}
  
//Driver Code
class GFG {
  
     //Main Method
     static void Main( string [] args)
     {
         C c = new C();
         I2 i2 = new C();
  
         //Invokes set accessor
         c.X = 10;
  
         //Invokes get accessor
         Console.WriteLine( "Value of x set using X" +
                              " from I1 is " + c.X);
  
         //Call to the X method
         Console.WriteLine( "Value returned by I2.X()" +
                                     " is " + i2.X());
     }
}

输出如下:

Value of x set using X from I1 is 10
Value returned by I2.X() is 0

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: