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.