2

一个程序集向外界公开了几个接口(IFirst, ISecond, ),即这些接口是.IThirdpublic

现在,作为实现细节,所有这些对象都有一个共同的特征,由接口描述IBase。我不想IBase公开,因为这可能会在未来的实现中发生变化,并且与我的程序集的用户完全无关。

显然,公共接口不能从内部接口派生(给我一个编译器错误)。

有什么方法可以表达IFirst, ISecond, 并且IThird仅从内部的角度来看有一些共同点吗?

4

2 回答 2

4

不在 C# 中

您能做的最好的事情就是让您的同时实现内部和公共接口。

于 2013-02-17T15:29:17.263 回答
1

正如安德鲁上面所说。只是为了扩展,这里有一个代码示例:

public interface IFirst
{
    string FirstMethod();
}

public interface ISecond
{
    string SecondMethod();
}

internal interface IBase
{
    string BaseMethod();
}

public class First: IFirst, IBase
{
    public static IFirst Create()  // Don't really need a factory method;
    {                              // this is just as an example.
        return new First();
    }

    private First()  // Don't really need to make this private,
    {                // I'm just doing this as an example.
    }

    public string FirstMethod()
    {
        return "FirstMethod";
    }

    public string BaseMethod()
    {
        return "BaseMethod";
    }
}

public class Second: ISecond, IBase
{
    public static ISecond Create()  // Don't really need a factory method;
    {                               // this is just as an example.
        return new Second();
    }

    private Second()  // Don't really need to make this private,
    {                 // I'm just doing this as an example.
    }

    public string SecondMethod()
    {
        return "SecondMethod";
    }

    public string BaseMethod()
    {
        return "BaseMethod";
    }
}
于 2013-02-17T16:12:40.183 回答