【问题标题】:C# 7.0 - Achieve multiple inheritance with default implementationC# 7.0 - 使用默认实现实现多重继承
【发布时间】:2020-07-01 13:03:40
【问题描述】:

问题是:如何同时提供多重继承(C# 不允许)和默认方法实现(C#

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

我写的示例代码如下(但请不要过多关注实现细节,仅将其视为描述性场景):

public abstract class Message
{
    protected Guid _id;
    protected string _body;
    protected Message(string body)
    {
        _body = body;
    }
    public override string ToString()
    {
        return _id + "|" + _body;
    }
}
public abstract class ReceivableMessage : Message
{
    private string _receivedFrom;
    public ReceivableMessage(string body, string receivedFrom)
    : base(body)
    {
        _receivedFrom = receivedFrom;
    }
    public string GenerateReply()
    {
        //do some generation
        return "";
    }
}
public abstract class SendableMessage : Message
{
    private string _sendTo;
    public SendableMessage(string body, string sendTo)
    : base(body)
    {
        _sendTo = sendTo;
    }
    public bool CheckReply(ReceivableMessage reply)
    {
        //do some check
        return true;
    }
}

那么我有几个ReceivableMessageSendableMessage 的具体实现;但现在我想添加具体类AckMessageNackMessage...它们应该同时扩展ReceivableMessageSendableMessage(因为两者都可以接收和发送)...我怎样才能做到这一点? 作为额外的 cmets,我考虑了两种可能性,但放弃了它们:

  1. 用 3 个接口替换 3 个抽象类。不行,因为我会丢失公共方法实现ToStringGenerateReplyCheckReply,我应该在所有具体类以及公共字段_id_body_receivedFrom 中重复它们和_sendTo(即使从技术上讲这可以避免用属性替换它们)。
  2. 提供AckReceivableMessageNackReceivableMessage(继承自ReceivableMessage)以及AckSendableMessageNackSendableMessage(继承自SendableMessage)的具体实现。不行,这在我看来是代码重复。

【问题讨论】:

  • C# 中没有多重继承,即使在 8 中也是如此。DIM 不提供多重继承,也不能替代抽象类
  • @PanagiotisKanavos 我知道 8.0 版中没有多重继承 (C# 不允许)(我没有指定任何版本,因此不适用于所有版本)。关于 DIM achronim,我很抱歉,但我不知道您指的是什么。
  • 你为什么首先考虑继承?继承不是代码重用机制。你有一堆消息。你想以同样的方式对待其中一些人吗?一个或多个接口将是一个好主意。扩展方法可以为每个接口提供额外的功能。
  • 该功能是默认接口成员(因此是 DIM),而不是默认方法实现
  • I know there is no multiple inheritance in version 8.0 您仍然考虑使用默认接口成员作为实现多重继承的一种方式。这就是它的用途。通过这种方式,这将是一种实现特征的方法,只能通过接口本身访问。对于消费者来说,这类似于在接口上使用扩展方法

标签: c# interface abstract-class multiple-inheritance


【解决方案1】:

我使用动态调度创建了多重继承。这是实现大规模定制系统属性的一种过于复杂的方法。您从根本上想要的是集成来自多个模块的处理的能力。制造架构允许容器以两种方式无限增长(这些术语来自业务):

  • 纵向:扩展流程(而不是子类化)
  • 横向:添加新进程(而不是继承或继承)

多重继承的另一个问题是它引入了歧义。当两个类都被继承时,不清楚应该调用哪个方法。

    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();
        }
    }

}

大规模定制允许您动态地集成处理和更改触发顺序。这当然是上面硬编码形式的替代方案,其中生产过程及其触发顺序是在编译时构建的。我的 R 文章涵盖了大规模定制,但我没有提供它的源代码。

我是 POWER 的开发人员,这是一种制造架构和有效使用它的规则。我的文章指导程序员如何使用源代码正确建模协作工作。

http://www.powersemantics.com/power.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多