简体   繁体   中英

simulating multiple inheritance in C#

so i saw this example from stackoverflow to implement multiple inheritance by using interfaces.

interface ILARGESimulator
{
}

interface IUDPClient
{
}

class UDPClient : IUDPClient
{
}

class LargeSimulator : ILARGESimulator
{
}

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

}

The guy said "Unfortunately you will need to write wrapper methods to the members. Multiple inheritance in C# does not exist. You can however implement multiple interfaces."

Why do we inherit from both interfaces anyway?

class RemoteLargeSimulatorClient : IUDPClient, ILargeSimulator

If you are having a has-a relationship and calling the base objects on the derived class, why do even have to write :IUDP, ILargeSimulator ? wouldn't it be simply

class RemoteLargeSimulatorClient 
{

is good?

If you don't base the class on the interfaces then you can't pass it as a IUDPClient or ILargeSimulator to other code. When he said you need to add the implentations manually he was basically suggesting you do this:

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

Then you can create an object instance of RemoteLargeSimulatorClient and use it as either a ILargeSimulator or 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);
}

Multiple class inheritance does not exist in C#. Otherwise can be inherit from multiple interfaces. Your example is try solve multiple class inheritance.

First you full understand an Interface. Why are interfaces useful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM