【问题标题】:How to implement multiple interfaces with same methods inhereted from parent interfaces如何使用从父接口继承的相同方法实现多个接口
【发布时间】:2014-02-21 14:47:56
【问题描述】:

考虑以下接口:

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 接口。 IHeatingElement 具有与 IPowerSwitch 相同的方法。那么如何实现 IHeatingElement 的 SwitchOn 和 SwitchOff。 如果我尝试实现 IPowerSwitch.SwitchOff() 之类的东西,我会收到错误

显式接口声明中的“IPowerSwitch.SwitchOff”不是接口的成员。

我想要做的是,当引发电源开关事件时,应该引发加热事件。并且当加热关闭时,应引发电源开关关闭事件。

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

【问题讨论】:

  • 并非所有事情都需要通过继承来实现。
  • 你不需要重新声明接口ISwitchable继承ISwitch/IComponent所以你的IPowerSwitch/IHeatingElement可以继承ISwitchable

标签: c# oop


【解决方案1】:

正如@Peter-ritchie 在 cmets 中所说,“并非所有事情都需要通过继承来实现”。在您当前的代码中,您试图说 Kettle 是一种电源开关和加热元件。我想你想说的是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 {}

【讨论】:

  • +1 这应该是公认的答案。提问者把这个问题混为一谈,因为他对待接口就像对待抽象类一样。
  • 同意,这是设计上的问题。
【解决方案2】:

一个类不能多次实现一个接口。恐怕这也包括任何继承的接口。

您必须将您的方法移动到所需的接口实现,才能按照您想要的方式工作。

【讨论】:

  • 克罗诺,感谢您的快速回复。我无法更改界面中的任何内容。我必须按原样实现它们。
【解决方案3】:

IPowerSwitch.SwitchOff 无法编译,因为SwitchOffISwitchable 的成员,并且您正在使用显式接口实现。

要么摆脱显式接口实现(只在你的类中实现SwitchOff而不是IPowerSwitch.SwitchOff)或实现ISwitchable.SwitchOff

【讨论】:

    【解决方案4】:

    你想要的本质上是多重继承。 .NET 不支持它。

    但是你可以用聚合代替继承。

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

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 1970-01-01
      • 2011-08-13
      • 2020-12-05
      • 2011-01-23
      • 2011-11-21
      • 1970-01-01
      • 2019-08-04
      相关资源
      最近更新 更多