📌  相关文章
📜  C#|如何实现具有相同方法名称的多个接口

📅  最后修改于: 2021-05-29 17:26:58             🧑  作者: Mango

像类一样, Interface可以将方法属性,事件和索引器作为其成员。但是接口将仅包含成员的声明。接口成员的实现将由隐式或显式实现接口的类给出。
C#允许使用相同的方法名称实现多个接口。为了理解如何使用相同的方法名称实现多个接口,我们举一个例子。在此示例中,我们采用两个具有相同方法名称的G1G2

现在,在名为Geeks的类中实现这些接口并定义mymethod()方法,并且当用户尝试调用此方法时,它会产生错误,因为我们没有告诉编译器该方法属于哪个接口。

例子:

// C# program to illustrate the concept
// of how to inherit multiple interfaces
// with the same method name
using System;
  
// Interface G1 and G2
// contains same method
interface G1 {
  
    // interface method
    void mymethod();
}
  
interface G2 {
  
    // interface method
    void mymethod();
}
  
// 'Geeks' implements both
// G1 and G2 interface
class Geeks : G1, G2 {
  
    // Defining method
    // this statement gives an error
    // because we doesn't specify
    // the interface name
    void mymethod()
    {
        Console.WriteLine("GeeksforGeeks");
    }
}
  
// Driver Class
public class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating object of Geeks
        // this statement gives an error
        // because we doesn't specify
        // the interface name
        Geeks obj = new Geeks();
  
        // calling method
        obj.mymethod();
    }
}

编译时错误:

要消除此错误,我们将使用方法名称(如G1.mymethod()指定接口的名称。它告诉编译器此方法属于G1接口。同样, G2.mymethod()告诉编译器此方法属于G2接口。

例子:

// C# program to illustrate the concept 
// of how to inherit multiple interfaces 
// with the same method name
using System;
  
// Interface G1 and G2 
// contains same method
interface G1 {
      
    // method declaration
    void mymethod();
}
  
interface G2 {
      
    // method declaration
    void mymethod();
}
  
// Geeks implements both 
// G1 and G2 interface
class Geeks : G1, G2
{
    // Here mymethod belongs to 
    // G1 interface
    void G1.mymethod()
{
    Console.WriteLine("GeeksforGeeks");
}
      
    // Here mymethod belongs to 
    // G2 interface
    void G2.mymethod()
{
    Console.WriteLine("GeeksforGeeks");
}
}
  
// Driver Class
public class GFG {
      
    // Main Method
    static public void Main () {
          
        // Creating object of Geeks
        // of G1 interface
        G1 obj = new Geeks();
          
        // calling G1 interface method
        obj.mymethod();
          
        // Creating object of Geeks
        // of G2 interface
        G2 ob = new Geeks();
          
        // calling G2 interface method
        ob.mymethod();
    }
}

输出 :

GeeksforGeeks
GeeksforGeeks

注意:您也可以在实现接口的类中将方法声明为public。但是困惑仍然存在,因为在大型程序中,用户无法区分实现哪种接口的方法。

例子:

// C# program to illustrate the concept
// of how to inherit multiple interfaces
// with the same method name by defining 
// the method as public in the class 
// which implements the interfaces.
using System;
  
// Interface G1 and G2
// contains same method
interface G1 {
  
    // interface method
    void mymethod();
}
  
interface G2 {
  
    // interface method
    void mymethod();
}
  
// 'Geeks' implement both
// G1 and G2 interface
class Geeks : G1, G2 {
  
    // Defining method as public
    public void mymethod()
    {
        Console.WriteLine("GeeksforGeeks");
    }
}
  
// Driver Class
public class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating object of Geeks
        Geeks obj = new Geeks();
  
        // calling method
        obj.mymethod();
    }
}

输出:

GeeksforGeeks