什么是当你编辑接口它叫什么名字? [英] What is it called when you edit an interface?

查看:140
本文介绍了什么是当你编辑接口它叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通过LitJSON库。在代码中有很多细分的像

I am going through a LitJSON library. In the code there are lots of segments like

 public class JsonData : IJsonWrapper, IEquatable<JsonData>

 #region ICollection Properties
            int ICollection.Count {
                get {
                    return Count;
                }
            }
    #end region

有关的方法我知道如何重写/重载的作品,但在上面的代码示例中写着:INT ICollection.Count

For a method I know how overriding/overloading works but in the example above the code reads: int ICollection.Count

我不熟悉的格式的方法签名。是编码器试图明确说明其ICollection.Count界面?

I'm not familiar with that format for a method signature. Is the coder trying to explicitly state its the ICollection.Count interface?

你能解释一下这是叫(被它覆盖还是?)。

Could you explain what this is "called" (is it overriding still?).

推荐答案

这就是所谓的显式接口实现。主要用于消除与目前在不同的接口也需要不同的实施同名成员。

It is called explicit interface implementation. Mainly used to disambiguate members with same name present in different interfaces which also needs different implementation.

考虑你有

interface ISomething1
{
    void DoSomething();
}

interface ISomething2
{
    void DoSomething();
}

class MyClass : ISomething1, ISomething2
{
    void ISomething1.DoSomething()
    {
        //Do something
    }

    void ISomething2.DoSomething()
    {
        //Do something else
    }
}



<同时为p>没有显式接口实现,你将不能够提供不同的实施的DoSomething的接口我们实施。

Without Explicit interface implementation you will not be able to provide different implementation of DoSomething for both the interfaces we implement.

如果你想实现一些接口,你想从客户端(高达一定程度上)隐藏它,你可以使用显式实现。 阵列的IList 接口明确,这就是它是如何隐藏 IList.Add IList.Remove 等,但你可以调用它,如果你将它转换为的IList 键入。但你最终得到在这种情况下例外。

If you want to implement some interface and you want to hide it from the client(upto some extent) you can use explicit implementation. Array class implements IList interface explicitly and that's how it hides IList.Add, IList.Remove etc. Nevertheless you can call it if you cast it to IList type. But you'll end up getting an Exception in this case.

通过明确的实现来实现成员不能通过类实例(即使在类)可见。你需要通过接口实例来访问它。

Members implemented via explicit implementations are not visible via class instance(Even inside the class). You need to access it via interface instance.

MyClass c = new MyClass();
c.DoSomething();//This won't compile

ISomething1 s1 = c;
s1.DoSomething();//Calls ISomething1's version of DoSomething
ISomething2 s2 = c;
s2.DoSomething();//Calls ISomething2's version of DoSomething

这篇关于什么是当你编辑接口它叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆