LSP 的协变和逆变
LSP与协方差和逆变有何关系? 有什么关系吗? LSP 是协方差的一种形式吗?
What is the relationship between LSP and Covariance and Contravariance?
Is there any relationship? Is LSP a form of Covariance ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会说 里氏替换原理 是协方差的一种“形式”,只是通用的协方差允许 LSP 以通用关系表示。从 C# 4 / .NET 4 开始,您可以将
IEnumerable
视为IEnumerable
并且应该能够这样做而不会出现任何令人讨厌的意外 - 这基本上就是 LSP 所要求的。I wouldn't say that Liskov's Substitution Principle is a "form" of covariance, so much as generic covariance allows LSP to be expressed in generic relationships. As of C# 4 / .NET 4, you can treat an
IEnumerable<Banana>
as anIEnumerable<Fruit>
and should be able to do so without any nasty surprises - which is what LSP requires, basically.