gpt4 book ai didi

c# - 如何使用从父接口(interface)继承的相同方法实现多个接口(interface)

转载 作者:太空狗 更新时间:2023-10-29 21:09:11 25 4
gpt4 key购买 nike

考虑以下接口(interface):

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 { }

我在这样的类中实现了 IPowerSwitch:

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

}

}

现在我想在这个类中实现 IHeatingElement 接口(interface)。 IHeatingElement 具有与 IPowerSwitch 相同的方法。那么我如何实现 IHeatingElement 的 SwitchOn 和 SwitchOff。如果我尝试实现 IPowerSwitch.SwitchOff() 之类的东西,我会收到错误

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

我想要做的是,当引发电源开关事件时,应该在此之后引发加热事件。当加热关闭时,应引发电源关闭事件。

这是我的第一个问题,所以如果问题中有问题,请指导我。提前感谢您的帮助。

最佳答案

正如@Peter-ritchie 在评论中所说,“并非所有内容都需要通过继承来实现”。在您当前的代码中,您试图说 Kettle 是一种电源开关和加热元件。我想你想说的是 水壶 电源开关和加热元件。这称为合成

相反,您可以像这样构造 Kettle 对象:

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 {}

关于c# - 如何使用从父接口(interface)继承的相同方法实现多个接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21937532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com