Liskov替换原理是否适用于从抽象类继承的子类型? [英] Does the Liskov Substitution Principle apply to subtype which inherited from abstract class?

查看:78
本文介绍了Liskov替换原理是否适用于从抽象类继承的子类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

松散地说,《里斯科夫替代原理》指出,派生类可以代替基类而不会影响用户. 如果基类是抽象类,这意味着没有用户使用基类的实例,那么Liskov继承限制是否仍适用于派生类?

loosely speaking, Liskov Substitution Principle states that a derived class can be substitute in place of the base class without affecting the user. In the case when the base class is an abstract class, which means no user is using an instance of the base class, does the Liskov inheritance restrictions still apply to the derived class?

推荐答案

仅仅因为您不能实例化特定的类并不意味着您不能使用它.在这种情况下,调用代码使用抽象基类作为操作所依据的合同的定义.从这个意义上讲,从基类派生的每个类都应该相对于基类定义的接口是可互换的,因此,Liskov仍然适用.实际上,这就是为什么要为具有某些常见行为的类的集合提供抽象基类的主要原因之一,因此您可以根据基类接口定义操作,而不必关心哪个派生类您实际上正在操作.

Just because you can't instantiate a particular class does not mean that you can't use it. In this scenario, the calling code is using the abstract base class as the definition of the contract under which it operates. In that sense, every class that derives from the base class ought to be interchangable with respect to the interface defined by the base class, so yes Liskov still applies. In fact, this is one primary reason why you would want to have an abstract base class for a collection of classes that have some common behavior -- so you can define operations in terms of the base class interface and not care about which derived class that you are actually operating on.

这篇关于Liskov替换原理是否适用于从抽象类继承的子类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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