加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#界面问题

发布时间:2020-12-15 07:48:04 所属栏目:百科 来源:网络整理
导读:我有以下代码: // IMyInterface.csnamespace InterfaceNamespace{ interface IMyInterface { void MethodToImplement(); }} . // InterfaceImplementer.csclass InterfaceImplementer : IMyInterface{ void IMyInterface.MethodToImplement() { Console.Wri
我有以下代码:
// IMyInterface.cs

namespace InterfaceNamespace
{
    interface IMyInterface
    {
        void MethodToImplement();
    }
}

.

// InterfaceImplementer.cs
class InterfaceImplementer : IMyInterface
{
    void IMyInterface.MethodToImplement()
    {
        Console.WriteLine("MethodToImplement() called.");
    }
}

这段代码编译得很好(为什么?).但是,当我尝试使用它时:

// Main.cs

    static void Main()
    {
        InterfaceImplementer iImp = new InterfaceImplementer();
        iImp.MethodToImplement();
    }

我明白了:

06003

即从外部看不到MethodToImplement.但是,如果我做了以下更改:

// InterfaceImplementer.cs
class InterfaceImplementer : IMyInterface
{

    public void MethodToImplement()
    {
        Console.WriteLine("MethodToImplement() called.");
    }
}

然后Main.cs编译也很好.为什么这两者有区别?

解决方法

到 implementing an interface explicitly,您正在创建一个只能通过强制转换到接口来调用的私有方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读