Interface inheritance. Does not implement interface error

一笑奈何 提交于 2021-01-27 06:54:43

问题


I have a problem with using inherited interface. I will explain my problem on example below. Let's say I have Interface IFlyable:

public interface IFlyable
{
    IVerticalSpeed Speed { get; set; }
}

It contains IVerticalSpeed interface. I created another interface called ISpeed which inherits from IVerticalSpeed interface:

public interface ISpeed : IVerticalSpeed
{
    int MaxSpeed { get; set; }
}

In next step I created a class Fly which implement IFlyable interface:

public class Fly : IFlyable
{
    public IVerticalSpeed Speed { get; set; }
}

Everything is fine... but what if I wanted to replace IVerticalSpeed interface to ISpeed interface which inherits from IVerticalSpeed interface?

public class Fly : IFlyable
{
    public ISpeed Speed { get; set; }
}

I thought that everything should be fine because my ISpeed interface is IVertialSpeed interface + anything that ISpeed interface contatins. But that is not right. I get error which says: "Fly does not implement interface member IFlyable.Speed (...). Why?


回答1:


All answers show you possible solutions but none actually answer the important question here:

I thought that everything should be fine because my ISpeed interface is IVertialSpeed interface + anything that ISpeed interface contatins. But that is not right. I get error which says: "Fly does not implement interface member IFlyable.Speed (...). Why?

You've said it yourself. ISpeed is an IVerticalSpeed, but not all IVerticalSpeeds are ISpeeds so you are not satisfying the contract at all.

What would happen if your code was allowed and I wanted to do the following:

public interface IMyOtherSpeed: IVerticalSpeed { ... }
IFlyable myFly = new Fly();
IMyOtherSpeed mySpeed = new MyOtherSpeed();

myFly.Speed = mySpeed; //Runtime error, mySpeed is not an ISpeed?!?

You see the issue now? You'd be breaking the interface's contract, becuase your class only accepts ISpeed when it should accept any IVerticalSpeed.




回答2:


If you use generics you can do something like

public interface IFlyable<out T> : where T IVerticalSpeed
{ T Speed {get;set;} }

Then in your class you would do something like

public class Fly : IFlyable<SomeSpeedClass>
{
  public SomeSpeedClass Speed{get;set}
}



回答3:


Because your property's signature must stay 100% the same. You can implement a new property, as a workaround. But it has it's limitations, too.

You can do this if your inheritance would work both ways. You already know, all ISpeeds are IVerticalSpeeds. But that's not the case the other way round. Not all IVerticalSpeeds are ISpeeds. So we can give your class another method, but it will face a problem at exactly that point:

  public interface IVerticalSpeed
  {
    int Value { get; set; }
  }

  public interface IFlyable
  {
    IVerticalSpeed Speed { get; set; }
  }

  public interface ISpeed : IVerticalSpeed
  {
    int MaxSpeed { get; set; }
  }

  public class Fly : IFlyable
  {
    public ISpeed Speed { get; set; }

    IVerticalSpeed IFlyable.Speed
    {
      get
      {
        return this.Speed;
      }
      set
      {
        // Wow, wait, you want to SET an IVerticalSpeed,
        // but not every IVerticalSpeed is an ISpeed... what now?
        // this.Speed = value;
      }
    }
  }

This is not a problem of code, this is a design problem. What do you want to do, if someone sets an IVerticalSpeed to your Fly? Because your interface says that's allowed. Maybe you don't want setters?




回答4:


Instead of

public class Fly : IFlyable
{
    public ISpeed Speed { get; set; }
}

You should match the IFlyable interface

public class Fly : IFlyable
{
    public IVerticalSpeed Speed { get; set; }
}

Interfaces are not inheritance, must be matched exactly.

Edit: Let me rephrase, interfaces that implementing other interfaces is not inheritance, hence the error. Interfaces must me matched exactly, that's is why it raises the error (ISpeed != IVerticalSpeed)




回答5:


You haven't shown IVerticalSpeed, but if you have control of it consider

public interface IFlyable<out TSpeed>
    where TSpeed : IVerticalSpeed
{
    TSpeed Speed { get; set; }
}

then implemented in Fly like this

public class Fly : IFlyable<ISpeed>
{
    public ISpeed Speed { get; set; }
}


来源:https://stackoverflow.com/questions/42510659/interface-inheritance-does-not-implement-interface-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!