【发布时间】:2013-12-17 02:01:27
【问题描述】:
我在 Library1 中有几个类:上面的类使用的 A 类、B 类等。
public class A
{
int VarA1;
}
public class B
{
List<A> SetA {get; set; }
void MethodB1()
{
...
this.SetA = new List<A>;
...
}
}
现在我想为那个 Library1 提供一个接口,这样就可以有其他的实现了。调用接口库ILibrary:
public interface IA
{
int VarA1 {get; set; }
}
public interface IB
{
List<IA> SetA { get; set; }
void MethodB1();
}
Library1 中正在实现的功能:
public class A : IA
{
int VarA1{ get; set; }
}
public class B : IB
{
List<IA> SetA {get; set; }
void MethodB1()
{
...
this.SetA = new List<A>;
...
}
}
但是我收到了这段代码的编译错误:
Library1.B.SetA 无法实现 ILibrary.IB.SetA,因为它没有匹配的返回类型。
我也试过了:
public class B : IB
{
List<A> SetA {get; set; }
void MethodB1()
{
...
this.SetA = new List<A>;
...
}
}
但这并没有解决问题(同样的错误消息)。为什么会出现此错误,我该如何解决?
【问题讨论】:
-
添加了更多详细信息,因此问题复制了我面临的问题。
-
问题还是一样。为了符合接口,返回的参数类型必须与接口中声明的内容完全匹配。请参阅我的更新答案。
-
您的代码仍然包含几个错误,并且绝对无法复制您所描述的问题。接口不能有字段:
public interface IA { int VarA1; }是不可能的。你在 SetA 和 ASet 之间切换了几次 -
@HugoRune 感谢 cmets。我更正了我的代码。