C# object-oriented design principles (two) the principle of substitution

1. The definition of the Richter substitution principle:

1. Inheritance must ensure that the properties possessed by the superclass are still valid in the subclass
2. Mainly explain when inheritance should be used and when should not be used

2. The role of the Richter substitution principle:

1. The Richter substitution principle is one of the important ways to realize the principle of opening and closing.
2. It overcomes the shortcomings of poor reusability caused by rewriting the parent class in inheritance.
3. It is the guarantee of the correctness of the action. That is, the extension of the class will not introduce new errors to the existing system, reducing the possibility of code errors.
4. Strengthen the robustness of the program, and at the same time, it can achieve very good compatibility when changing, improve the maintainability and scalability of the program, and reduce the risk introduced when changing requirements.

Three, the realization of the principle of Richter substitution:

1. Subclasses can extend the functions of the parent class, but cannot change the original functions of the parent class . In other words: when a subclass inherits a parent class, in addition to adding new methods to complete the new functions, try not to override the methods of the parent class.
2. If the program violates the Richter substitution principle, the object of the inherited class will have an operating error where the base class appears. At this time, the correction method is: cancel the original inheritance relationship and redesign the relationship between them.

Guess you like

Origin blog.csdn.net/MrLsss/article/details/109231191