In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about what is the Richter scale replacement principle. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Richter substitution principle is one of the basic principles of object-oriented design, which advocates the use of "Abstraction" and "Polymorphism" to change the static structure into dynamic structure to maintain the closeness of the design. "abstraction" is a function provided by language, and "polymorphism" is realized by inheritance semantics.
The Richter substitution principle (Liskov Substitution Principle,LSP) was proposed by Ms. Liskov of the computer Science Lab of the Massachusetts Institute of Technology in an article "data abstraction and hierarchy" (Data Abstraction and Hierarchy) published at the 1987 Summit on object-oriented Technology (OOPSLA). She proposed that inheritance must ensure that the properties of the superclass still hold in the subclass (Inheritance should ensure that any property proved about supertype objects also holds for subtype objects).
The Richter substitution principle mainly expounds some principles about inheritance, that is, when inheritance should be used, when inheritance should not be used, and the principles implied in it. Richter substitution is the basis of inheritance reuse, which reflects the relationship between base class and subclass, complements the opening and closing principle, and standardizes the concrete steps to realize abstraction.
The role of the Richter scale substitution principle
The main functions of the Richter scale substitution principle are as follows.
1. The Richter scale replacement principle is one of the important ways to realize the opening and closing principle.
2. It overcomes the disadvantage 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.
The realization method of Richter scale substitution principle
The Richter substitution principle is generally speaking: subclasses can expand the functions of the parent class, but can not change the original function of the parent class. That is to say: when a subclass inherits the parent class, try not to override the parent class's methods except to add new methods to complete the 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 polymorphisms are used frequently, the probability of error in running the program will be very high.
If the program violates the Richter substitution principle, the object of the inherited class will have a run error where the base class appears. At this time, the correction method is to cancel the original inheritance relationship and redesign the relationship between them.
As an example of the principle of Richter scale substitution, the most famous one is "a square is not a rectangle". Of course, there are many similar examples in life. For example, penguins, ostriches and kiwi are classified as birds from a biological point of view, but from the perspective of class inheritance, because they cannot inherit the function that "birds" can fly, so they cannot be defined as subclasses of "birds". Similarly, since "balloon fish" cannot swim, it cannot be defined as a subcategory of "fish"; "toy cannon" cannot blow up the enemy, so it cannot be defined as a subcategory of "cannon" and so on.
The following is an example of "kiwi is not a bird" to illustrate the Richter scale replacement principle.
[example 2] the application of Richter substitution principle in the example of "kiwi birds are not birds".
Analysis: birds generally fly, such as swallows flying at a speed of about 120 kilometers per hour. But kiwi in New Zealand cannot fly because of their degraded wings. If you want to design an example, calculate the time it takes for these two kinds of birds to fly 300 kilometers. Obviously, the result of testing this code with a swallow is correct and the time required can be calculated, but when testing a kiwi, there will be a "division by zero exception" or "infinity", which is obviously not in line with expectations, as shown in figure 1.
The program code is as follows:
Package principle;public class LSPtest {public static void main (String [] args) {Bird bird1=new Swallow (); Bird bird2=new BrownKiwi (); bird1.setSpeed (120); bird2.setSpeed (120); System.out.println ("if you fly 300km:"); try {System.out.println ("swallows will fly" + bird1.getFlyTime (300) + "hours.") System.out.println ("kiwi will fly" + bird2.getFlyTime (300) + "hours.") ;} catch (Exception err) {System.out.println ("error occurred!");} / / Bird class Bird {double flySpeed; public void setSpeed (double speed) {flySpeed=speed;} public double getFlyTime (double distance) {return (distance/flySpeed) }} / / class Swallow extends Bird {} / / Kiwi birds class BrownKiwi extends Bird {public void setSpeed (double speed) {flySpeed=0;}}
The running result of the program is as follows:
If you fly 300 kilometers, the swallow will fly for 2.5 hours. Kiwi will fly for Infinity hours.
The reason for the error is that Kiwi rewrites the bird's setSpeed (double speed) method, which violates the Richter substitution principle. The right thing to do is to cancel the original inheritance of kiwi and define the more general parents of birds and kiwi, such as animals, which have the ability to run. Although the kiwi's flight speed is 0, but the running speed is not 0, the time it takes to run 300 kilometers can be calculated.
The class diagram is shown in figure 2.
The above is the editor for you to share what is the Richter replacement principle, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.