简体   繁体   English

无法实现接口成员,因为它没有匹配的返回类型

[英]Cannot implement interface member because it doesn't have matching return type

I'm working on an abstract algebra library for C#, but am having trouble with implementing interfaces. 我正在为C#开发抽象代数库,但是在实现接口时遇到了麻烦。 I have gotten the implementation to work for certain groups, but attempting to create rings is giving me some serious problems. 我已经为某些小组使用了该实现,但是尝试创建环会给我带来一些严重的问题。 In particular, I've got the following: 特别是,我有以下内容:

public class Scaffolding {

public interface IMonoid<T> : ISemiGroup<T> {
        T Identity { get; set; }
    }

public interface IGroup<T> : IMonoid<T> {
        T Inverse(T a);
    }

 public interface IRing<T> {
       IGroup<T> AdditiveStructure { get; set; }
       IMonoid<T> MultiplicativeStructure { get; set; }
    }
}

 public class ModularMonoid : Scaffolding.IMonoid<int> {
       // Implements all necessary monoid properties
    }

 public class ModularGroup : Scaffolding.IGroup<int> {
       // Implements all necessary group properties
    }

 public class ModularRing : Scaffolding.IRing<int> {
    public ModularGroup AdditiveStructure { get; set; }
    public ModularMonoid MultiplicativeStructure { get; set; }

    // Implement ring-specific properties
}

I get an error stating that 'ModularRing' does not implement interface member 'Scaffolding.IRing.AdditiveStructure'. 我收到一条错误消息,指出“ ModularRing”未实现接口成员“ Scaffolding.IRing.AdditiveStructure”。 'ModularRing.AdditiveStructure' cannot implement 'Scaffolding.IRing.AdditiveStructure' because it does not have the matching return type of 'Scaffolding.IGroup'. 'ModularRing.AdditiveStructure'无法实现'Scaffolding.IRing.AdditiveStructure',因为它没有匹配的返回类型'Scaffolding.IGroup'。 I get a similar error for the MultiplicativeStructure. 我对MultiplicativeStructure遇到类似的错误。 This is strange to me, because both the ModularGroup and ModularMonoid implement IGroup and IMonoid respectively. 这对我来说很奇怪,因为ModularGroup和ModularMonoid都分别实现了IGroup和IMonoid。

Yes, those classes implement the interfaces, but that interface doesn't say "The type of the AdditiveStructure property is something that implements IGroup<T> " - it says that the type of the AdditiveStructure property is IGroup<T> . 是的,这些类实现了接口,但是该接口没有说“ AdditiveStructure属性的类型是实现IGroup<T>东西”,而是说AdditiveStructure属性的类型 IGroup<T> To implement the interface, you have to match return types exactly . 要实现该接口,您必须完全匹配返回类型。

If you want to be able to implement the interface like that, you'd need to change the interface, potentially like this: 如果您希望能够实现这样的接口,则需要更改该接口,可能是这样的:

public interface IRing<T, TGroup, TMonoid>
    where TGroup : IGroup<T>
    where TMonoid : IMonoid<T>
{
   TGroup AdditiveStructure { get; set; }
   TMonoid MultiplicativeStructure { get; set; }
}

Then implement it as: 然后将其实现为:

public class ModularRing : Scaffolding.IRing<int, ModularGroup, ModularMonoid>
{
    public ModularGroup AdditiveStructure { get; set; }
    public ModularMonoid MultiplicativeStructure { get; set; }        
}

Alternatively, you should consider making the properties read-only. 或者,您应考虑将属性设置为只读。 That way, if you're happy with the ModularRing users only using the IGroup<int> and IMonoid<int> definitions (rather than depending on anything extra exposed on ModularGroup and ModularMonoid ) then you could stick with just the single type parameter, which would simplify things quite a lot. 这样,如果您仅使用IGroup<int>IMonoid<int>定义对ModularRing用户感到满意(而不是依赖于ModularGroupModularMonoidModularGroup ModularMonoid ),则可以只使用单个类型参数,即会简化很多事情。 For example: 例如:

public interface IMonoid<T> : ISemiGroup<T>
{
    T Identity { get; }
}

public interface IGroup<T> : IMonoid<T>
 {
    T Inverse(T a);
}

public interface IRing<T>
{
   IGroup<T> AdditiveStructure { get; }
   IMonoid<T> MultiplicativeStructure { get; }
}

Implementation: 执行:

public class ModularRing : Scaffolding.IRing<int>
{
    public IGroup<int> AdditiveStructure { get; } = new ModularGroup();
    public IMonoid<int> MultiplicativeStructure { get; } = new ModularMonoid();
}

(Or accept them in the constructor; I don't know enough about what you're trying to do with them.) (或者在构造函数中接受它们;对于您要如何使用它们,我不太了解。)

暂无
暂无

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

相关问题 无法实现接口成员,因为它没有匹配的返回类型 - Cannot implement interface member because it does not have the matching return type 无法实现接口成员,因为它没有匹配的返回类型 - unable to implement interface member 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> BaseClass 不能实现 interface.variable 因为它没有匹配的返回类型 - BaseClass cannot implement interface.variable because it does not have the matching return type 接口设计实现错误:…无法实现…,因为它没有匹配的返回类型 - Interface design implementation error: … cannot implement … because it does not have a matching return type 无法实现接口,因为任务没有匹配的返回类型<custom class>方法</custom> - Cannot implement interface because does not have matching returning type for Task<Custom class> method “...”无法实现接口成员,因为它不是公共的 - “…” cannot implement an interface member because it is not public 无法实现接口成员,因为它不是公共的 - Cannot implement an interface member because it is not public 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? 通用类型的接口实现失败,因为“没有匹配的返回类型”错误 - Interface implementation fails for generic type, because of “does not have the matching return type” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM