我有一些接口和继承的麻烦。在这里我的问题:无法实现接口成员,因为它没有匹配的返回类型
我有两个接口:
public interface IElementA
{
List<IElementA> Child { get; }
}
// The goal is to add some properties to the main interface
public interface IElementB : IElementA
{
string Name { get; }
}
和类实现IElementB
public class ElementB : IElementB
{
protected List<ElementB> m_Child = new List<ElementB>();
public List<ElementB> Child { get { return m_Child; } }
public string Name { get { return "element B"; }
}
然后我得到了错误:
'ElementB' does not implement interface membre 'IElementA.Child'.
'ELementB.Child' cannot implement 'IElementA.Child' because it does not have the matching return type of 'List<IElementA>'."
我明白,我需要写
public List<IElementA> Child { get { return m_Child; } }
并且知道模板技巧,但它只适用于不同类型的IElementA列表。
你有什么想法来解决我的问题吗?
问候 JM
'列表'与列表'不是同一种类型,这就是为什么你会收到编译错误。 –
DavidG
看*协变* – Rahul
DavidG有正确答案 – Picnic8