c# - 实现定义相同方法的两个接口(interface)的类

标签 c# oop interface-implementation

//inteface i11 
 public interface i11
    {
        void m11();
    }
//interface i22
    public interface i22
    {
         void m11();
    }

//class ab implements interfaces i11 and i22
    public class ab : i11, i22
    {
        public void m11()
        {
            Console.WriteLine("class");
        }
    }

现在在 Main() 方法中我们创建了一个类 ab 的对象 将调用哪个接口(interface)方法请解释所有。

最佳答案

只有一个方法实现,所以很明显它会被调用,而不管引用的类型如何。即:

i11 o = new ab();
o.m11();

i22 o = new ab();
o.m11();

实际上是一样的。

ab.m11 满足两个接口(interface)中方法的签名。另外,您没有使用 explicit interface implementation ,所以这不是一个因素。

关于c# - 实现定义相同方法的两个接口(interface)的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4638218/

相关文章:

c# - 此 XmlWriter 不支持 base64 编码数据

vb.net - 即使定义了函数,我也必须在类中实现函数的错误

java - 实现接口(interface)并扩展不同项目的类

c# - 多次实现尝试

c# - 如何将元素添加到 c# XSD 生成的序列化类中的数组?

c# - 将模板内容转换为颜色叠加

c# - 使用 WaitForSeconds() 时使协程更快吗?

c++ - 从复合模式结构中删除元素

python - 推荐命名法 : a better term than "parent"?

c++ - 解释复制构造函数示例