编译器说,我不执行我的接口,但我是谁? [英] Compiler says I am not implementing my interface, but I am?

查看:124
本文介绍了编译器说,我不执行我的接口,但我是谁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有两个命名空间。其中包含我的接口和一个包含了实现类。像这样的:

Okay, I have two namespaces. One contains my interface and one contains the implementing class. Like this:

namespace Project.DataAccess.Interfaces
{
    public interface IAccount
    {
        string SomeMethod();
    }
}

namespace Project.DataAccess.Concrete
{
    class Account : Project.DataAccess.Interfaces.IAccount
    {
        string SomeMethod()
        {
            return "Test";
        }
    }
}

通过这个code我得到一个错误:

With this code I get an error:

Project.DataAccess.Concrete.Account不实现接口成员Project.DataAccess.Interfaces.IAccount.SomeMethod。 Project.DataAccess.Concrete.Account.SomeMethod无法实现接口成员,因为它是不公开

'Project.DataAccess.Concrete.Account' does not implement interface member 'Project.DataAccess.Interfaces.IAccount.SomeMethod'. 'Project.DataAccess.Concrete.Account.SomeMethod' cannot implement an interface member because it is not public

如果我做的类和方法公开它工作正常。但是,如果我不是限定方法名称与接口名称,修复它。为什么?例如:

If I make the class and method public it works fine. But if I instead qualify the method name with the interface name, that fixes it too. Why? Ex:

namespace Project.DataAccess.Concrete
{
    class Account : Project.DataAccess.Interfaces.IAccount
    {
        string IAccount.SomeMethod()
        {
            return "Test";
        }
    }
}

为什么这个解决?为什么它必须是公共的,如果我不这样做?

Why does this fix it? And why does it have to be public if I don't do that?

感谢你在前进。

我的坏,帐户并从IAccount继承。我只是写上飞的样本。

My bad, Account does inherit from IAccount. I just wrote the sample on the fly.

我很清楚地意识到,使其成为公众修复它。制作方法签名是这样未做任何公开修正它:

I am well aware that making it public fixes it. Making the method signature look like this WITHOUT making anything public fixes it:

string IAccount.SomeMethod()

为什么?

推荐答案

接口的实现必须是公开的或明确的:

Interface implementations need to be public or explicit:

公开:

class Account : IAccount
{
    public string SomeMethod()
    {
        return "Test";
    }
}

明确:

class Account : IAccount
{
    string IAccount.SomeMethod()
    {
        return "Test";
    }
}

在C#中的默认访问修饰符是私人如果不指定访问修饰符。

The default access modifier in C# is private if you do not specify the access modifier.

这篇关于编译器说,我不执行我的接口,但我是谁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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