为什么要显式实现接口? [英] Why implement interface explicitly?

查看:24
本文介绍了为什么要显式实现接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,明确实现接口的好用例到底是什么?

So, what exactly is a good use case for implementing an interface explicitly?

是否只是为了让使用该类的人不必在智能感知中查看所有这些方法/属性?

Is it only so that people using the class don't have to look at all those methods/properties in intellisense?

推荐答案

如果你实现了两个接口,都使用相同的方法和不同的实现,那么你必须显式实现.

If you implement two interfaces, both with the same method and different implementations, then you have to implement explicitly.

public interface IDoItFast
{
    void Go();
}
public interface IDoItSlow
{
    void Go();
}
public class JustDoIt : IDoItFast, IDoItSlow
{
    void IDoItFast.Go()
    {
    }

    void IDoItSlow.Go()
    {
    }
}

这篇关于为什么要显式实现接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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