简体   繁体   English

BaseClass 不能实现 interface.variable 因为它没有匹配的返回类型

[英]BaseClass cannot implement interface.variable because it does not have the matching return type

EDIT: SOLVED.编辑:解决了。 I am an idiot sometimes.我有时是个白痴。 See my self-answer below...请参阅下面的我的自我回答...

I'm working on the following C# .Net4.5 code using interfaces and polymorphism我正在使用接口和多态性处理以下 C# .Net4.5 代码


public interface IFile
{
    List<string> Contents {get;set;}
}

public class File : IFile
{
    public List<string> Contents {get;set;}
}

public interface ICommandFile : IFile
{
    new List<ICommandFileLine> Contents {get;set;} /*ICommandFileLine is defined in the code as well, but I don't believe this is pertinent to the issue I am having.*/
}

public class CommandFile : File, ICommandFile
{
    public new List<ICommandFileLine> Contents {get;set;}
}

When I try and do the above, the pre-compiler complains:当我尝试执行上述操作时,预编译器会抱怨:

CommandFile does not implement interface memeber "ICommandFile.Contents". CommandFile 不实现接口成员“ICommandFile.Contents”。 'File.Contents' cannot implement "ICommandFile.Contents" because it does not have the matching return type of "List[ICommandFileLine]" “File.Contents”无法实现“ICommandFile.Contents”,因为它没有匹配的“List[ICommandFileLine]”返回类型

Interface member "List ICommandFile.Contents" is not implemented.未实现接口成员“List ICommandFile.Contents”。

I don't understand.我不明白。 I'm using the new keyword, surely this should indicate I wish to encapsulate the base class and define the new variable type?我正在使用 new 关键字,这肯定表明我希望封装基类并定义新的变量类型吗?

Try a generic interface - IFile<T> .尝试通用接口 - IFile<T> Then ICommandFile implements IFile<CommandLine>.然后ICommandFile实现IFile<CommandLine>. Get the idea?明白了吗?

接口成员实现不应像您在 CommandFile 类中那样标记为“新”),这意味着实现类中的成员与从接口继承的成员无关。

I realised what I did wrong not 5 minutes after posting:发布后不到 5 分钟,我就意识到我做错了什么:

List[ICommandFile] Contents EQUALS!?列表[ICommandFile] 内容等于!? No, {get;set;} !不, {get;set;}

Changed:更改:

new List[ICommandFileLine] Contents = new ListICommandFileLine; new List[ICommandFileLine] Contents = new ListICommandFileLine;

in my local code to:在我的本地代码中:

new List[ICommandFileLine] Contents {get;set;}新列表 [ICommandFileLine] 内容 {get;set;}

How embarrassing...多么尴尬...

I'm still relatively new to implementing interfaces and making these mistakes from time to time.我对实现接口和不时犯这些错误还是比较陌生。

暂无
暂无

声明:本站的技术帖子网页,遵循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> 接口设计实现错误:…无法实现…,因为它没有匹配的返回类型 - Interface design implementation error: … cannot implement … because it does not have a matching return type 无法实现接口成员,因为它没有匹配的返回类型 - 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? 通用类型的接口实现失败,因为“没有匹配的返回类型”错误 - Interface implementation fails for generic type, because of “does not have the matching return type” error 无法创建变量类型“Item”的实例,因为它没有 new() 约束 - Cannot create an instance of the variable type 'Item' because it does not have the new() constraint 无法使用集合初始化程序初始化类型,因为它没有实现ienumerable - cannot initialize type with a collection initializer because it does not implement ienumerable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM