Tag: function overloading

为什么接口实现不能返回更具体的类型?

如果接口指定了返回另一个接口的属性或方法,为什么第一个接口的实现不允许将返回类型“更改”为更具体的类型? 我们举一个例子来说明: interface IFoo { IBar GetBar(); } interface IBar { } class Foo : IFoo { // This is illegal, we are not implementing IFoo properly public Bar GetBar() { return new Bar(); } } class Bar : IBar { } 我知道如何让它发挥作用,这不是我关心的问题。 我可以: 将GetFoo()返回类型更改为IBar ,或 显式实现接口,只需从IFoo.GetBar()方法调用GetBar 我真正要问的是不仅允许上面的代码编译的原因。 有没有上述情况不符合IFoo规定的合同。