C# 在不同类中实现接口,为什么需要使用接口名作为限定符?

boot_black 2018-06-15 01:33:19
在不同类中实现接口,为什么需要使用接口名作为限定符?

我的理解:
cat和dog是完全不同的两个类,分别在两个不同的类中实现各自的接口,为什么还需要限定呢?

恳请各位指教,不胜感激!

interface ILiveBirth
{
string BabyCalled();
}

class animal
{
}

class cat : animal, ILiveBirth
{
string ILiveBirth.BabyCalled() //为什么不是string BabyCalled()?
{
return "name.";
}
}

class dog : animal, ILiveBirth
{
string ILiveBirth.BabyCalled() //为什么不是string BabyCalled()?
{
return "name.";
}
}
...全文
1086 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
圣殿骑士18 2018-06-19
  • 打赏
  • 举报
回复
你是想要看着爽的答案吧,没看出你哪里诚恳请教了。
xuzuning 2018-06-15
  • 打赏
  • 举报
回复
不是 需要使用接口名作为限定符 而是可以 使用接口名作为限定符 以满足不同应用场合的需要 显然 接口名.接口方法名 这样的定义,既满足了接口方法必须实现的要求,又满足了隐藏接口方法的目的
维秀斯丢丢 2018-06-15
  • 打赏
  • 举报
回复
了解一下,接口的显示和隐式实现。
exception92 2018-06-15
  • 打赏
  • 举报
回复
用来区分实现不同接口中相同的方法名称,不然编译器会报类似“xx不明确”的错误。
cheng2005 2018-06-15
  • 打赏
  • 举报
回复
接口的显式实现和隐式实现
正怒月神 2018-06-15
  • 打赏
  • 举报
回复
这是没写访问修饰符导致的。
public interface ILiveBirth
    {
        string BabyCalled();
    }

    public class animal
    {
    }

    public class cat :animal, ILiveBirth
    {
        public virtual string BabyCalled() //为什么不是string BabyCalled()?
        {
            return "name.";
        }

    }

    public class dog :animal, ILiveBirth
    {
        public virtual string BabyCalled()  //为什么不是string BabyCalled()?
        {
            return "name.";
        }
    }
本人QQ-554433626 2018-06-15
  • 打赏
  • 举报
回复
非公共方法不能从接口实现方法 你可以写成:

            public string BabyCalled() //为什么不是string BabyCalled()?
            {
                return "name.";
            }

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧