嵌套接口-这可能吗? [英] Nested Interfaces - is this possible?

查看:87
本文介绍了嵌套接口-这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作:

I'm trying to do something like this:

    public interface IBase
    {
        void BaseMethod();
    }
    
    public interface IDerived_1 : IBase
    {
        void Method_1();
    }

    public interface IDerived_2 : IBase
    {
        void Method_2();
    }

    public class Implementation : IDerived_1, IDerived_2
    {

        void IDerived_1.Method_1()
        {
            throw new NotImplementedException();
        }

        void IDerived_1.IBase.BaseMethod()
        {
            throw new NotImplementedException();
        }

        public void IDerived_2.Method_2()
        {
            throw new NotImplementedException();
        }

        void IDerived_2.IBase.BaseMethod()
        {
            throw new NotImplementedException();
        }


    }

现在当然不会编译-但您可以看到它的意图.

Now of course this will not compile - but you can see the intention.

IBase 可能是一个相当丰富的接口,具有大量其他派生"接口所需的方法/属性.而不是定义 IDervived_1 IDervived_2 都有这些定义,我想一次定义它们 IBase ,然后将其与每个接口 IDervived_1 关联, IDerived_2 .

IBase could be a fairly rich interface that has a host of methods/properties required by a large number of other, 'derived' interfaces. Rather than define IDervived_1 and IDervived_2 to each have these defintions, I want to define them once in IBase and then associate that with each interface IDervived_1 and IDerived_2.

那么有没有一种方法可以排除一组接口之间的共性,以便在一个地方定义这些共性,而不是在其他接口中一遍又一遍地重复进行方法定义的工作?

So is there a way to factor out commonalities among a set of interfaces so that these are defined in one place rather than repeating a load of method definitions over and over in the other interfaces?

IBase 方法 BaseMethod 的具体实现取决于它是否是 IDerived_1 IDerived_2 实现.

The specific implementation of the IBase method BaseMethod will vary depending upon whether it is the IDerived_1 or IDerived_2 implementation.

谢谢

Cap'n

推荐答案

一个选项不是同时实现两个接口,而是公开两个分别实现其中一个接口的属性.

One option is not to implement both interfaces, but rather expose two properties that each implement one of the interfaces.

public class Implementation
{
    public Implementation()
    {
        First = new FirstImplementation();
        Second = new SecondImplementation();
    }
    public IDerived_1 First { get; private set; }
    public IDerived_2 Second { get; private set; }

    private class FirstImplementation : IDerived_1
    {
        void IDerived_1.Method_1()
        {
            throw new NotImplementedException();
        }

        void IBase.BaseMethod()
        {
            throw new NotImplementedException();
        }
    }
    private class SecondImplementation : IDerived_2
    {
        void IDerived_2.Method_2()
        {
            throw new NotImplementedException();
        }

        void IBase.BaseMethod()
        {
            throw new NotImplementedException();
        }
    }
}


这篇关于嵌套接口-这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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