C#7.0-使用默认实现实现多重继承

C#7.0-使用默认实现实现多重继承,c#,interface,abstract-class,multiple-inheritance,C#,Interface,Abstract Class,Multiple Inheritance,问题是:如何提供多重继承(C#不允许)和默认方法实现(C#

问题是:如何提供多重继承(C#不允许)和默认方法实现(C#<8.0中的接口不允许)

一般情况下,当我有第一个抽象层,然后第二个抽象层生成两个不同的族/类别,最后是属于上述两个族/类别的具体实现

我写的示例代码如下(但请不要太多关注实现细节,只考虑这是一个描述性的场景):

然后我有了
ReceivableMessage
SendableMessage
的几个具体实现;但现在我想添加具体的类
AckMessage
NackMessage
…它们应该扩展
ReceivableMessage
SendableMessage
(因为两者都可以接收和发送)…我如何实现这一点?
作为补充意见,我考虑了两种可能性,但放弃了它们:

  • 用3个接口替换3个抽象类。不正常,因为我会丢失公共方法实现
    ToString
    GenerateReply
    CheckReply
    ,我应该在所有具体类中重复它们,以及公共字段
    \u id
    \u body
    \u从
    接收和
    \u发送到
    (即使在技术上可以避免将其替换为属性)
  • 提供
    AckReceivableMessage
    NackReceivableMessage
    (继承自
    ReceivableMessage
    ),以及
    AckSendableMessage
    NackSendableMessage
    (继承自
    SendableMessage
    )的具体实现。不正常,我觉得这是代码重复

  • 我通过使用动态调度创建了多重继承。这是一种完成大规模定制系统属性的过于复杂的方法。您基本上想要的是从多个模块集成处理的能力。制造体系结构允许容器无限增长——这些术语来自业务——在tw中o方式:

    • 垂直:扩展流程(而不是子类化)
    • 横向:添加新进程(而不是通过子类化或继承来实现)
    多重继承的另一个问题是它引入了模糊性。当两个类都被继承时,不清楚应该调用哪个方法

        class A
        {
            public void One() { ... }
        }
        class B
        {
            public void One() { ... }
        }
    
    然而,制造体系结构(如大规模定制)将流程建模为整个类,而不仅仅是方法,因此名称空间可以防止上述问题

    namespace A
    {
        class OneProduct { ... }
        class One : Producer<OneProduct>, IProcess { ... }
    }
    
    namespace B
    {
        class OneProduct { ... }
        class One : Producer<OneProduct>, IProcess { ... }
    }
    
    // example of a hardcoded process
    namespace IntegratingProcess
    {
        class MyProduct { ... }
        class MyProcess : Producer<OneProduct>, IProcess 
        {
            private A.One Machine1 { get; set; }
            private B.One Machine2 { get; set; }
            void D() { // allocate memory for all machines and product DTO }
            void O() { // binds Machine1 and Machine2 to MyProduct reference properties }
            void M()
            {
                Machine1.M();
                Machine2.M();
            }
        }
    
    }
    
    名称空间A
    {
    一类产品{…}
    第一类:生产者,IProcess{…}
    }
    命名空间B
    {
    一类产品{…}
    第一类:生产者,IProcess{…}
    }
    //硬编码过程的示例
    命名空间集成过程
    {
    类MyProduct{…}
    类MyProcess:生产者,IProcess
    {
    私有A.一台机器1{get;set;}
    私有B.一台机器2{get;set;}
    void D(){//为所有机器和产品DTO分配内存}
    void O(){//将Machine1和Machine2绑定到MyProduct引用属性}
    void M()
    {
    Machine1.M();
    Machine2.M();
    }
    }
    }
    
    大规模定制允许您动态地集成处理和更改触发顺序。这当然是上述硬编码形式的替代形式,其中生产过程及其触发顺序是在编译时构建的。我的R文章介绍了大规模定制,但我没有给出它的源代码

    我是POWER的开发者,POWER是一种制造体系结构,也是有效使用POWER的规则。我的文章指导程序员如何正确地用源代码对协作工作建模


    C#中没有多重继承,即使在8中也是如此。DIMs不提供多重继承,也不能替代抽象继承classes@PanagiotisKanavos我知道8.0版中没有多重继承(C#不允许)(我没有指定任何版本,因此不允许所有版本)关于Dims AkrimIM,我很抱歉,但我不知道你指的是什么。为什么你首先考虑继承?继承不是一个代码重用机制。你有一堆消息。你想用同样的方式对待其中的一些吗?一个或多个接口是个好主意。扩展方法可以提供额外的功能。每个接口的属性。该功能是默认接口成员(因此为DIM),而不是默认方法实现
    我知道8.0版中没有多重继承
    您仍然考虑使用默认接口成员作为实现多重继承的一种方式。这就是现在的情况。这样,它将是实现特征的一种方式,只能通过接口本身访问。对于消费者来说,这将类似于在接口上使用扩展方法
    namespace A
    {
        class OneProduct { ... }
        class One : Producer<OneProduct>, IProcess { ... }
    }
    
    namespace B
    {
        class OneProduct { ... }
        class One : Producer<OneProduct>, IProcess { ... }
    }
    
    // example of a hardcoded process
    namespace IntegratingProcess
    {
        class MyProduct { ... }
        class MyProcess : Producer<OneProduct>, IProcess 
        {
            private A.One Machine1 { get; set; }
            private B.One Machine2 { get; set; }
            void D() { // allocate memory for all machines and product DTO }
            void O() { // binds Machine1 and Machine2 to MyProduct reference properties }
            void M()
            {
                Machine1.M();
                Machine2.M();
            }
        }
    
    }