大家好。为什么不能用i2_c1类中特定类的返回值来实现getI?毕竟,这个特定的类实现了接口,正如我所想,编译器不会关心返回什么,主要是接口被实现。也许我做错了什么?这是代码:
interface i1
{
int f();
}
class i1_c1 : i1
{
public int f() { return 1; }
}
class i1_c2 : i1
{
public int f() { return 2; }
}
interface i2
{
i1 getI();
}
class i2_c1 : i2
{
public i1_c1 getI()
{
return new i1_c1();
}
}
此代码编译时出现错误:错误 CS0738 'i2_c1' does not implement interface member 'i2.getI()' 'i2_c1.getI()' cannot implement 'i2.getI()' because it does not have a对应的返回类型'我1'”。
UPD.:你能解释一下为什么我不能将返回类型设置为实现正在返回的接口的具体类吗?