Design Principle, 设计原则之LSP

时间:2021-08-14 11:59:03

 

The Liskov Substitution Principle (LSP)

Subclasses should be substitutable for their base classes.

子类应该可替代他们基类。

This principle was coined by Barbar Liskov in her work regarding data abstraction

and type theory. It also derives from the concept of Design by Contract (DBC) by

Bertrand Meyer.

这个原则由Barbar Liskov在他考虑关于数据抽象和分类理论的工作中创建,它也来源于Bertrand Meyer的紧缩设计(DBC) 概念。

The concept, as stated above, derived classes should be

substitutable for their base classes. That is, a user of a base class should continue to

function properly if a derivative of that base class is passed to it.

这个概念,像上面说的,派生类应该可以替代他们的基类。也就是说,如果派生类被传给一个应用基类的user类,它应该正常的继续它的功能。

Design Principle, 设计原则之LSP

 

In other words, if some function User takes an argument ot type Base, then as

shown in Listing 2-4, it should be legal to pass in an instance of Derived to that

function.

换句话说,如果user有一个基类的参数,那么如上图显示的,它应该被传递一个派生类的实例。