简体   繁体   English

C#中的多接口继承

[英]Multiple Interface inheritance in C#

I have two interfaces with same method 我有两个接口使用相同的方法

   interface FirstInterface
   {
     int add(int x, int y);
   }

   interface SecondInterface
   {
      int add(int x, int y);
   }

   class TestInterface:FirstInterface,SecondInterface
   {
     public TestInterface() {}

     public int add(int x, int y)
     {
        return x + y;
     }
   }

and in my main class 在我的班级

    static void Main(string[] args)
    {
        TestInterface t = new TestInterface();
        int result = t.add(3, 4);

    }

The compiler doesnt give any error and the result is displayed.. 编译器不会给出任何错误,并显示结果..

Is it right ?? 这样对吗 ?? shouldn't we use FirstInterface.add ?? 我们不应该使用FirstInterface.add ??

does it mean interfaces are resolved at compile time ?? 它是否意味着接口在编译时被解析

It's the subject of Explicit or Implicit implementation of interfaces. 它是接口的显式或隐式实现的主题。

If you implement Interface A and Interface B , and they both have a member called Foo , which is a method, then if you implement these interfaces in one class, your only method Foo would count for both of them. 如果你实现了Interface AInterface B ,并且它们都有一个名为Foo的成员,这是一个方法,那么如果你在一个类中实现这些接口,那么你唯一的方法就是Foo This is by design, and frankly, I think there is no other way around for this. 这是设计的,坦率地说,我认为没有别的方法可以解决这个问题。

To implement them separately, you have to use Explicit Interface Implementation , that is, you prefix the name of the Foo method with the name of the interface and a dot, like InterfaceA.Foo and InterfaceB.Foo . 要单独实现它们,必须使用Explicit Interface Implementation ,即,使用Explicit Interface Implementation名称和点(如InterfaceA.FooInterfaceB.FooFoo方法的名称添加前缀。

Quoting from C# specification : 引用C#规范

20.3 Fully qualified interface member names 20.3完全限定的接口成员名称

An interface member is sometimes referred to by a qualified interface member name. 接口成员有时由合格的接口成员名称引用。 A qualified interface member name consists of a name identifying the interface in which the member is declared, followed by a dot, followed by the name of the member. 合格的接口成员名称包含一个名称,该名称标识声明成员的接口,后跟一个点,后跟成员的名称。

This is implicit interface implementation, so the method is now associated with the instance of TestResult as well. 这是隐式接口实现,因此该方法现在也与TestResult的实例相关联。 This means you can call the method on both instances with the concrete class handle as well as instances with an IFirstInterface handle. 这意味着您可以使用具体的类句柄以及具有IFirstInterface句柄的实例在两个实例上调用该方法。

FirstInterface interfaceHandle = new TestInterface();
interfaceHandle.add();

Will also work 也会工作

Blog about Implementation. 关于实施的博客。

What you are doing here is implementing the interface implicitly.with implicit interface implementation you access the interface methods and properties as if they were part of the class itself. 你在这里做的是隐式实现接口。通过隐式接口实现,你可以访问接口方法和属性,就好像它们是类本身的一部分一样。

Whereas in explicit implementation you can access them by treating them as interface 而在显式实现中,您可以通过将它们视为接口来访问它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM