Role
It instructs us to inherit and derive correctly, and to reuse code reasonably! Defined
Subtypes must be able to replace their parent types and appear anywhere the parent class can appear.
This is to use the Polymorphic method of programming, that is, the GRASP mode of polymorphism.
If O1 for each object of type T1, there is a T2 object O2, so that all program P defined by T1 is substituted for O1 for all object O2, the behavior of the program P is not changed, then the type T2 is the subtype of type T1.
In other words, if a software entity uses a base class, it must apply to its subclasses, and it cannot detect the difference between a base class object and a subclass object at all.
In turn substitution is not tenable.
Main points of the Richter substitution principle:
1 The breach of LSP also potentially violates the OCP
2 The example of a square inheriting from a rectangle subtly violates this principle
3. When considering whether a particular design is appropriate, the solution cannot be viewed in isolation, and it must be viewed in the light of reasonable assumptions made by the user of the design.
4 The behavior of the object is the problem that the software really concerns, is a relation is the behavior
5 a derived class can only replace the original predecessor condition with an equal or weaker precondition, and only replace the original post condition with an equal or stronger precondition.
6 The completed function is less than the derived class of the base class usually cannot replace the base class and does not conform to the LSP
7) Derived classes should not throw exceptions that cannot be predicted by the base class, or violate the principle of LSP substitution
The Richter substitution principle can be expressed in layman's terms: If a base class object can be used in software, it must be able to use its subclass object. Replace the base class with its subclass, the program will not generate any errors and exceptions, the reverse is not true, if a software entity is using a subclass, then it is not necessarily able to use the base class. The principle of the Richter substitution is one of the important ways to realize the opening and closing principle. Because subclass objects can be used where the base class object is used, it is possible to use the base class type in the program to define the object, and then to determine its subclass type at run time, replacing the parent class object with the subclass object.