BaseClass无法实现interface.variable,因为它没有匹配的返回类型

问题描述 投票:-1回答:3

编辑:已解决。我有时候是个白痴。看下面我的自我回答......

我正在使用接口和多态来处理以下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;}
}

当我尝试执行上述操作时,预编译器会抱怨:

CommandFile没有实现接口memeber“ICommandFile.Contents”。 'File.Contents'无法实现“ICommandFile.Contents”,因为它没有匹配的返回类型“List [ICommandFileLine]”

未实现接口成员“List ICommandFile.Contents”。

我不明白。我正在使用new关键字,当然这应该表明我希望封装基类并定义新的变量类型?

c# inheritance interface polymorphism encapsulation
3个回答
1
投票

尝试通用接口 - IFile<T>。然后ICommandFile实施IFile<CommandLine>.得到这个想法?


0
投票

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


-1
投票

在发布后5分钟我意识到我做错了什么:

列表[ICommandFile]内容EQUALS!?不,{get; set;}!

更改:

new List [ICommandFileLine] Contents = new ListICommandFileLine;

在我的本地代码中:

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

多么尴尬...

我对于实现接口和不时犯这些错误仍然相对较新。

© www.soinside.com 2019 - 2024. All rights reserved.