从内部派生的公共接口?

4
一个程序集向外部世界公开了几个接口(IFirstISecondIThird),也就是说这些接口是public的。
现在,作为一种实现细节,所有这些对象都共享一个由接口IBase描述的共同特征。我不想将IBase公开,因为这可能会在未来的实现中发生变化,并且对于我的程序集的用户来说完全无关紧要。
显然,公共接口不能从内部接口派生(会导致编译器错误)。
有没有办法表达IFirstISecondIThird在内部视图中有某些共同点?
2个回答

4

不适用于C#

最好的做法是让你的实现内部和公共接口。


1

正如Andrew所说。为了扩展,这里有一个代码示例:

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";
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接