LSP 的协变和逆变

发布于 2024-09-26 19:37:20 字数 46 浏览 1 评论 0原文

LSP与协方差和逆变有何关系? 有什么关系吗? LSP 是协方差的一种形式吗?

What is the relationship between LSP and Covariance and Contravariance?
Is there any relationship? Is LSP a form of Covariance ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2024-10-03 19:37:20

我不会说 里氏替换原理 是协方差的一种“形式”,只是通用的协方差允许 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 an IEnumerable<Fruit> and should be able to do so without any nasty surprises - which is what LSP requires, basically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文