具有相同方法名称的两个接口

时间:2020-04-25 07:27:38

标签: c# .net

可以从两个接口为同名方法实现一个实现吗?

我坚信我们需要始终执行显式实现IFoo1.Bar(),但是下面的代码正在编译和运行。有什么想法吗?

using System;
namespace ConsoleApp1
{
    interface IFoo1
    {
        void Bar();
    }
    interface IFoo2
    {
        void Bar();
    }
    class Foo : IFoo1, IFoo2
    {
        public void Bar()
        {
            Console.WriteLine("Test");
        }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            Foo foo = new Foo();
            foo.Bar();

            IFoo1 iFoo1 = new Foo();
            iFoo1.Bar();

            IFoo2 iFoo2 = new Foo();
            iFoo2.Bar();
        }
    }
}

0 个答案:

没有答案