How to implement multiple interfaces with the same methods that execute from parent interfaces

Consider the following interfaces:

public interface IComponent { } public interface ISwitch : IComponent { bool IsOn { get; } event EventHandler SwitchedOff; event EventHandler SwitchedOn; } public interface ISwitchable : ISwitch, IComponent { void SwitchOff(); void SwitchOn(); } public interface IPowerSwitch : ISwitchable, ISwitch, IComponent { } public interface IHeatingElement : ISwitchable, ISwitch, IComponent { } 

I implemented IPowerSwitch in the class as follows:

 public class Kettle : IPowerSwitch { event EventHandler PowerOnEvent; event EventHandler PowerOffEvent; object objectLock = new Object(); public bool IsPowerOn; public Kettle() { IPowerSwitch p = (IPowerSwitch)this; p.SwitchedOn += new EventHandler(On_PowerOn_Press); p.SwitchedOff += new EventHandler(On_PowerOff_Press); } void ISwitchable.SwitchOff() { EventHandler handler = PowerOffEvent; if (handler != null) { handler(this, new EventArgs()); } } void ISwitchable.SwitchOn() { EventHandler handler = PowerOnEvent; if (handler != null) { handler(this, new EventArgs()); } } bool ISwitch.IsOn { get { return IsPowerOn ; } } event EventHandler ISwitch.SwitchedOff { add { lock (objectLock) { PowerOffEvent += value; } } remove { lock (objectLock) { PowerOffEvent -= value; } } } event EventHandler ISwitch.SwitchedOn { add { lock (objectLock) { PowerOnEvent += value; } } remove { lock (objectLock) { PowerOnEvent -= value; } } } protected void On_PowerOn_Press(object sender, EventArgs e) { if (!((IPowerSwitch)sender).IsOn) { Console.WriteLine("Power Is ON"); ((Kettle)sender).IsPowerOn = true; ((IPowerLamp)this).SwitchOn(); } else { Console.WriteLine("Already ON"); } } protected void On_PowerOff_Press(object sender, EventArgs e) { if (((IPowerSwitch)sender).IsOn) { Console.WriteLine("Power Is OFF"); ((Kettle)sender).IsPowerOn = false; ((IPowerLamp)this).SwitchOff(); } else { Console.WriteLine("Already OFF"); } } } 

Now I want to implement the IHeatingElement interface in this class. IHeatingElement has the same methods as IPowerSwitch. So how can I implement SwitchOn and SwitchOff from IHeatingElement. If I try to implement something like IPowerSwitch.SwitchOff (), I get an error

'IPowerSwitch.SwitchOff' in the explicit interface declaration is not a member of the interface.

What I want to do is that when the power is turned on at the event, the heat after that should be raised. And when the heating is off, turn off the power off event.

This is my first question here, so please guide me if something is wrong with the question. Thank you in advance for your help.

+6
source share
4 answers

As Peter Richie said in the comments, "not everything should be implemented with inheritance." In your current code, you are trying to say that Kettle is a type of power switch and heating element. I think you want to say that Kettle has a power switch and a heating element. This is called composition .

Instead, you structure the Kettle object like this:

 public class Kettle : ISwitchable { private IPowerSwitch Power; private IHeatingElement Heat; public Kettle() { Power = new PowerSwitch(); Heat = new HeatingElement(); } public void SwitchOn() { Power.SwitchOn(); Heat.SwitchOn(); } // And so on. } public class PowerSwitch : IPowerSwitch {} public class HeatingElement : IHeatingElement {} 
+8
source

A class cannot implement an interface more than once. This also includes any legacy interfaces, and I'm afraid.

You will need to transfer your methods to the desired implementation of the interface so that it works the way you want.

+1
source

IPowerSwitch.SwitchOff does not compile because SwitchOff is a member of ISwitchable and you are using an explicit interface implementation.

Either get rid of the explicit interface implementation (and just implement SwitchOff in your class instead of IPowerSwitch.SwitchOff ), or implement ISwitchable.SwitchOff

+1
source

What you want is essentially multiple inheritance. It is not supported by .NET.

But you can replace inheritance with aggregation.

 class KettlePowerSwitch : IPowerSwitch { } class KettleHeatingElement : IHeatingElement { } class Kettle { public IPowerSwitch PowerSwitch = new KettlePowerSwitch(); public IHeatingElement HeatingElement = new KettleHeatingElement(); } 
+1
source

All Articles