继承自具有相同方法签名的多个接口的类

Man*_*ish 8 c# oop interface

说,我有三个接口:

public interface I1
{
    void XYZ();
}
public interface I2
{
    void XYZ();
}
public interface I3
{
    void XYZ();
}
Run Code Online (Sandbox Code Playgroud)

继承自这三个接口的类:

class ABC: I1,I2, I3
{
      // method definitions
}
Run Code Online (Sandbox Code Playgroud)

问题:

它编译得很好,也运行得很好!这是否意味着这个单一方法实现足以继承所有三个接口?

  • 如何实现所有三个接口的方法并调用它们?像这样的东西:

    ABC abc = new ABC();
    abc.XYZ(); // for I1 ?
    abc.XYZ(); // for I2 ?
    abc.XYZ(); // for I3 ?
    
    Run Code Online (Sandbox Code Playgroud)

我知道它可以使用显式实现完成,但我无法调用它们.:(

Dea*_*ing 8

如果使用显式实现,则必须将对象强制转换为要调用其方法的接口:

class ABC: I1,I2, I3
{
    void I1.XYZ() { /* .... */ }
    void I2.XYZ() { /* .... */ }
    void I3.XYZ() { /* .... */ }
}

ABC abc = new ABC();
((I1) abc).XYZ(); // calls the I1 version
((I2) abc).XYZ(); // calls the I2 version
Run Code Online (Sandbox Code Playgroud)