C#接口实现关系只是“Can-Do”。关系? [英] C# Interface Implementation relationship is just "Can-Do" Relationship?

查看:365
本文介绍了C#接口实现关系只是“Can-Do”。关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天有人告诉我,C#中的接口实现只是Can-Do关系,而不是Is-A关系。这与我长期相信LSP(Liskov Substitution Principle)相冲突。我一直认为所有的继承应该意味着Is-A的关系。

Today somebody told me that interface implementation in C# is just "Can-Do" relationship, not "Is-A" relationship. This conflicts with my long-time believing in LSP(Liskov Substitution Principle). I always think that all inheritance should means "Is-A" relationship.

所以,如果接口实现只是一个可以做的关系。如果有一个界面IHuman和IEngineer,并且一个类Programmer继承自IHuman& IEngineer?当然,程序员是IHuman和IEngineer。

So, If interface implementation is just a "Can-Do" relationship. What if there is a interface "IHuman" and "IEngineer", and one class "Programmer" inherits from "IHuman" & "IEngineer"? Surely, a "Programmer" Is A "IHuman" and A "IEngineer".

如果它只是Can-Do关系,是否意味着我们不能指望程序员实例行为在被视为IHuman并被视为Ihuman时可能会有所不同IEngineer?

If it is just "Can-Do" relationship, does it mean we cannot expect the "Programmer" instance behavior may be different between when treated as a IHuman and treated as IEngineer?

推荐答案

根据我的经验,想到is-a和can-并没有太大帮助做关系。你很快就会遇到问题。它基本上是现实世界和OO之间的阻抗不匹配。然而,实际上很多人都在谈论对现实世界进行建模,你从根本上需要了解类型之间的关系在你正在使用的平台上意味着什么。

In my experience it doesn't really help that much to think of "is-a" and "can-do" relationships. You rapidly get into problems. It's an impedance mismatch between the real world and OO, basically. However much people actually talk about modeling the real world, you fundamentally need to understand what the relationships between types mean on the platform you're using.

有时可以使用接口作为能力,有时它们可​​以代表更多正常的是 - a关系。我不会太在意它 - 只要确保你明白他们能做什么,不能做什么。

Sometimes interfaces can be used as capabilities, and sometimes they can represent more of a normal "is-a" relationship. I wouldn't get too hung up about it - just make sure you understand what they can do and what they can't.

这篇关于C#接口实现关系只是“Can-Do”。关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆