Current location - Training Enrollment Network - Mathematics courses - Richter substitution principle
Richter substitution principle
In object-oriented programming, Liskov substitution principle is a special definition of subtype. It was first put forward by Barbara Liskov in a speech entitled "Abstraction and Hierarchy of Data" delivered at a meeting in 1987.

Richter's replacement principle: Where any base class can appear, subclasses can also appear. Popular understanding: subclasses can expand the functions of the parent class, but they cannot change the original functions of the parent class. In other words, when a subclass inherits from its parent class, it should try not to rewrite the methods of the parent class except adding new methods to complete new functions.

If the new function is completed by rewriting the parent class, it is easy to write, but the reusability of the whole inheritance system will be poor, especially when polymorphism is often used, the probability of program running error will be high.

A square is not a rectangle. In the field of mathematics, there is no doubt that a square is a rectangle. It is a rectangle with equal length and width. Therefore, we have developed a software system related to geometric figures, which can naturally make squares inherit rectangles.

superiority

(1) subclasses can implement abstract methods of the parent class, but they cannot override non-abstract methods of the parent class.

(2) Subclasses can add their own unique methods.

(3) When the method of the subclass overloads the method of the parent class, the precondition of the method (that is, the input/input parameters of the method) is looser than the input parameters of the method of the parent class.

(4) When a subclass's method implements a parent's method (rewriting/overloading or implementing an abstract method), the post-condition of the method (that is, the output/return value of the method) is stricter or the same as that of the parent.