【问题标题】:simulating multiple inheritance in C#在 C# 中模拟多重继承
【发布时间】:2013-12-03 02:54:43
【问题描述】:

所以我从 stackoverflow 看到了这个例子 使用接口实现多重继承。

interface ILARGESimulator
{
}

interface IUDPClient
{
}

class UDPClient : IUDPClient
{
}

class LargeSimulator : ILARGESimulator
{
}

class RemoteLargeSimulatorClient : IUDPClient, ILargeSimulator
{
    private IUDPClient client = new UDPClient();
    private ILARGESimulator simulator = new LARGESimulator();

}

那家伙说 “不幸的是,您需要为成员编写包装方法。C# 中不存在多重继承。但是您可以实现多个接口。”

为什么我们还是要从这两个接口继承?

class RemoteLargeSimulatorClient : IUDPClient, ILargeSimulator

如果你有一个has-a关系并调用派生类的基对象,为什么还要写:IUDP, ILargeSimulator? 会不会很简单

class RemoteLargeSimulatorClient 
{

好吃吗?

【问题讨论】:

标签: c# multiple-inheritance


【解决方案1】:

如果您不将类基于接口,则不能将其作为 IUDPClient 或 ILargeSimulator 传递给其他代码。当他说你需要手动添加实现时,他基本上是在建议你这样做:

interface ILargeSimulator
{
    void Simulator_Method_1();
    void Simulator_Method_2();
}

public class UDPClient : IUDPClient
    {
    public void UDPClient_Method_1() { /* do something here */ }
    public void UDPClient_Method_2() { /* do something here */ }
}

interface IUDPClient
{
    void UDPClient_Method_1();
    void UDPClient_Method_2();
}

public class LargeSimulator : ILargeSimulator
{
    public void Simulator_Method_1() { /* do something here */ }
    public void Simulator_Method_2() { /* do something here */ }
}

public class RemoteLargeSimulatorClient : IUDPClient, ILargeSimulator
{
    private IUDPClient client = new UDPClient();
    private ILargeSimulator large = new LargeSimulator();

    public void Simulator_Method_1() { this.large.Simulator_Method_1(); }
    public void Simulator_Method_2() { this.large.Simulator_Method_2(); }
    public void UDPClient_Method_1() { this.client.UDPClient_Method_1(); }
    public void UDPClient_Method_2() { this.client.UDPClient_Method_2(); }
}

然后您可以创建 RemoteLargeSimulatorClient 的对象实例并将其用作 ILargeSimulator 或 IUDPClient:

static void DoSomethingWithClient(IUDPClient client) { /* etc */ }
static void DoSomethingWithSimulator(ILargeSimulator simulator) { /* etc */ }

static void Main(string[] args)
{
    RemoteLargeSimulatorClient foo = new RemoteLargeSimulatorClient();
    DoSomethingWithClient(foo);
    DoSomethingWithSimulator(foo);
}

【讨论】:

  • 感谢您的回答。现在问题变成了:为什么要声明 private IUDPClient client = new UDPClient();私人 ILargeSimulator large = new LargeSimulator();为什么不只是 private UDPCClient client = new UDPCClient() private LargeSimulator large = new LargeSimulator() public void Simulator_Method_1() { large.Simulator_Method_1();} 等等。
  • 因为在实际应用程序中,您不会在此处创建这些对象,您可以将 IUDPClient 和 ILargeSimulator 变量传递给构造函数,或者让依赖注入框架为您初始化它们。这样,您可以设置它,以便在您的商业应用程序中将 UDPClient 传递到该类中,但在您运行单元测试框架以检查您的代码是否有效时传递一些其他类(例如 UDPDummyClient)。在单元测试中,UDPDummyClient 类会伪装成您的代码的 IUDPClient,但实际上不会通过真实网络发送任何内容。
  • 为了澄清一下,他试图通过将调用重定向到实现对象来演示如何伪造多重继承(尽管不是很好,因为它很混乱且容易出错)。他一开始没有正确展示它们是如何创建的,那是一个完全不同的话题。
【解决方案2】:

C# 中不存在多个class 继承。否则可以从多个接口继承。您的示例是尝试解决多类继承。

首先你完全理解一个接口。 Why are interfaces useful

【讨论】:

    猜你喜欢
    • 2020-06-08
    • 2011-03-13
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多