[英]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.