If a method returns an interface, what does it mean?

后端 未结 4 1373
眼角桃花
眼角桃花 2021-01-18 11:38

I see many methods that specify an interface as return value. Is my thought true that it means: my method can return every class type that inherits from that interface? if n

4条回答
  •  梦谈多话
    2021-01-18 11:59

    Yes, it means that the only thing you know about the object that is returned is that it implements the interface.

    In fact, the actual type of the object may not even be accessible to the calling code. It could be a private type in a separate assembly.

    And in fact, the method may return a different type from one invocation to the next (as in the case of an abstract factory).

提交回复
热议问题