简体   繁体   English

接口设计实现错误:…无法实现…,因为它没有匹配的返回类型

[英]Interface design implementation error: … cannot implement … because it does not have a matching return type

I have a few classes in Library1: Class A, Class B, etc. that are used by classes above. 我在Library1中有几个类:A类,B类等,上述类使用。

public class A 
{  
    int VarA1;  
}

public class B
{  
    List<A> SetA {get; set; }
    void MethodB1()
    {
            ...
            this.SetA = new List<A>;
            ...
    }
}

Now I want to have an interface for that Library1, so there can be other implementations. 现在,我想为该Library1提供一个接口,以便可以有其他实现。 Calling the interface library ILibrary: 调用接口库ILibrary:

public interface IA
{  
    int VarA1 {get; set; }  
}

public interface IB
{  
    List<IA> SetA { get; set; }
    void MethodB1();
}

which are being implemented in Library1: 在Library1中正在实现:

public class A : IA
{  
    int VarA1{ get; set; }  
}

public class B : IB
{  
    List<IA> SetA {get; set; }
    void MethodB1()
    {
            ...
            this.SetA = new List<A>;
            ...
    }
}

But I am getting a compilation error with this code: 但是我收到此代码的编译错误:

Library1.B.SetA cannot implement ILibrary.IB.SetA because it does not have a matching return type. Library1.B.SetA无法实现ILibrary.IB.SetA,因为它没有匹配的返回类型。

I also tried: 我也尝试过:

public class B : IB
{  
    List<A> SetA {get; set; }
    void MethodB1()
    {
            ...
            this.SetA = new List<A>;
            ...
    }
}

But that didn't resolve the issue (same error message). 但这并不能解决问题(相同的错误消息)。 Why is this giving this error and how can I resolve it? 为什么会出现此错误,我该如何解决?

As the error message indicates, in order to comply with the interface, B.MethodB1 will have to return a type of IA , like this: 如错误消息所示,为了遵守该接口, B.MethodB1将必须返回IA类型,如下所示:

public class B : IB
{  
    public IA MethodB1() { ... }  
}

An alternative is to declare IB as a generic interface like this: 另一种选择是将IB声明为通用接口,如下所示:

public interface IB<out T> where T : IA
{  
    public T MethodB1();  
}

And then B like this: 然后B像这样:

public class B : IB<A>
{  
    public A MethodB1() { ... }  
}

Regarding your updated question, your issue is still the same, and so are the solutions. 关于您更新的问题,您的问题仍然相同,解决方案也是如此。 Either do something like this: 请执行以下操作:

public class B : IB
{  
    public List<IA> SetA {get; set; }
    public void MethodB1()
    {
            ...
            this.SetA = new List<IA>();
            ...
    }
}

Or like this: 或像这样:

public interface IB<T> where T : IA
{  
    List<T> SetA { get; set; }
    void MethodB1();
}

public class B : IB<A>
{  
    public List<A> SetA {get; set; }
    public void MethodB1()
    {
            ...
            this.SetA = new List<A>();
            ...
    }
}

The implementation of a method from an interface must return the exact same type, and IA is not the same as A. They have to absolutely match so it must return IA. 从接口实现方法必须返回完全相同的类型,并且IA与A不同。它们必须完全匹配,因此必须返回IA。 However your B implementation of MethodB1 does not have a body and the class is not abstract so you will still get an error. 但是,您的MethodB1的B实现没有主体,并且该类也不是抽象的,因此仍然会出现错误。

List<A> does not inherit from or implement List<IA> . List<A>不继承或实现List<IA>

The interface IB requires a List<IA> property. 接口IB需要List<IA>属性。 The class B has a List<A> property. 类B具有List<A>属性。

Consider: 考虑:

interface IAnimal()
{
  public void HaveLunch();
}

class Zebra:IAnimal
class Lion:IAnimal


interface IZoo
{
  List<IAnimal> Zebras {get;set;}
  List<IAnimal> Lions {get;set;}
}

public class Zoo
  : IZoo //does not work
{
  List<Zebra> Zebras {get;set;}
  List<Lion> Lions {get;set;}
}


//...
IZoo myZoo = new Zoo;
myZoo.Zebras.Add(new Lion());  //because of this.

the other answers have already said why this will not work: 其他答案已经说明了为什么这行不通:

  • An inherited property has to use the exact same type as the parent 继承的属性必须使用与父属性完全相同的类型
  • You cannot assign List<IA> myList = new List<A>(); 您不能分配List<IA> myList = new List<A>(); ,
    you must use the same type. 您必须使用相同的类型。 This is because List is not covariant. 这是因为List不是协变的。

So you will have to use IA instead of A throughout your code, and only use the public methods accessible to IA 因此,在整个代码中,您将必须使用IA而不是A,并且只能使用IA可访问的公共方法

There is however a way to make it work, while still having access to A, if you change the type of ASet to IEnumerable<IA> : 但是,如果将ASet的类型更改为IEnumerable<IA> ,则有一种方法可以使它在仍然可以访问A的同时工作:

  • you can assign IEnumerable<IA> myList = new List<A>(); 您可以分配IEnumerable<IA> myList = new List<A>();
    because IEnumerable supports covariance , since C# 4.0 因为IEnumerable 支持协方差 ,所以从C#4.0开始

IEnumerable does not support all the methods that List does, for example you cannot add more elements to an IEnumerable. IEnumerable不支持List所支持的所有方法,例如,您不能向IEnumerable中添加更多元素。 This is unfortunately necessary to make covariance work. 不幸的是,这是使协方差起作用的必要条件。 You can however internally use a List inside class B, and only present the IEnumerable to the outside. 但是,您可以在B类内部内部使用List,并且只能将IEnumerable呈现给外部。

public interface IB
{  
    IEnumerable<IA> SetA { get; set; }
    void MethodB1();
}

public class B : IB
{  
    private List<A> listA;

    public IEnumerable<IA> SetA 
    {
        get {return listA;} 
        set {throw new NotImplementedException();} 
        // the setter is somewhat inelegant, 
        // if you do not need the setter 
        // you should probably leave it out of the interface entirely
    }

    public void MethodB1()
    {
            this.listA = new List<A>()
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法实现接口成员,因为它没有匹配的返回类型 - Cannot implement interface member because it does not have the matching return type 通用类型的接口实现失败,因为“没有匹配的返回类型”错误 - Interface implementation fails for generic type, because of “does not have the matching return type” error 无法实现接口成员,因为它没有匹配的返回类型 - unable to implement interface member because it does not have the matching return type BaseClass 不能实现 interface.variable 因为它没有匹配的返回类型 - BaseClass cannot implement interface.variable because it does not have the matching return type 无法实现接口成员,因为它没有与List匹配的返回类型<IInterface> - Cannot implement interface member because it does not have the matching return type of List<IInterface> 无法实现接口成员,因为它没有匹配的返回类型 - Cannot implement interface member because it doesn't have matching return type 无法实现接口,因为任务没有匹配的返回类型<custom class>方法</custom> - Cannot implement interface because does not have matching returning type for Task<Custom class> method System.Windows.Window.Activate()无法实现Microsoft.Office.Interop.Word.Window.Activate(),因为它没有匹配的返回类型? - System.Windows.Window.Activate() cannot implement Microsoft.Office.Interop.Word.Window.Activate() because it does not have the matching return type? 使用接口和泛型时没有匹配的返回类型错误 - Does not have matching return type error when using interfaces and generics 如何将接口的实现作为返回类型返回? - How to return an implementation of an interface with interface as return type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM