栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

当您编辑界面时,这叫什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

当您编辑界面时,这叫什么?

这称为显式接口实现。主要用于消除在不同接口中存在的具有相同名称的成员的歧义,这也需要不同的实现。

认为你有

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

没有Explicit接口实现,您将无法

DoSomething
为我们实现的两个接口提供不同的实现。

如果要实现某个接口,并且想在客户端中隐藏它(在某种程度上),则可以使用显式实现。

Array
IList
显式实现接口,这就是隐藏的方式
IList.Add
IList.Remove
等等。不过,如果将其强制转换为
IList
类型,则可以调用它。但是在这种情况下,您最终将获得异常。

通过显式实现实现的成员在类实例中不可见(即使在类内部)。您需要通过接口实例访问它。

MyClass c = new MyClass();c.DoSomething();//This won't compileISomething1 s1 = c;s1.DoSomething();//Calls ISomething1's version of DoSomethingISomething2 s2 = c;s2.DoSomething();//Calls ISomething2's version of DoSomething


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/386806.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号