C#中的接口继承 [英] Interface Inheritance in C#

查看:156
本文介绍了C#中的接口继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用接口的继承是一个很好的编码实践。



IBird 界面有很多鸟与 Fly()行为一起定义的行为。现在,如果Bird类(比如说,Ostrich)实现了这个接口,它必须不必要地实现Fly()行为(鸵鸟不会飞)。



IBird 界面分为两个不同的界面, IBird IFlyingBird ,IFlyingBird继承了IBird。



这是一个很好的编码实践。

Is it a good coding practice to use inheritance of Interfaces.

the IBird interface has many bird behaviours defined along with the Fly() behaviour. Now, if a Bird class (say, Ostrich) implements this interface, it has to implement the Fly() behaviour unnecessarily (Ostrich doesn't fly).

The IBird Interface is broken down into two different interfaces, IBird and IFlyingBird, where the IFlyingBird inherits IBird.

Is it a good coding practice to do like this.

推荐答案

你的问题在这篇伟大的文章中被特别引用:

我如何向妻子解释OOD [ ^ ]



主题:Liskov的替代原则

Your question is being referenced specifically in this great article:
How I explained OOD to my wife[^]

See the sction "Topic: Liskov's Substitution Principle":
引用:

这里,KingFisher类扩展了Bird基类和他nce继承了Fly()方法,非常好。



现在看一下下面的例子:



修正Liskov替换原则的类层次结构

Ostrich是一只Bird(绝对是!)因此它继承了Bird类。现在,它可以飞吗?没有!在这里,设计违反了LSP。 ...

Here, the KingFisher class extends the Bird base class and hence inherits the Fly() method, which is pretty good.

Now take a look at the following example:

Corrected class hierarchy of Liskov Substitution Principle
Ostrich is a Bird (definitely it is!) and hence it inherits the Bird class. Now, can it fly? No! Here, the design violates the LSP. ...





干杯,

Edo



Cheers,
Edo


这篇关于C#中的接口继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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