This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.
define
Liskov Substitution Principle (LSP), officially defined as follows: If for each type of the object S o1, have of type T object o2, makes all the procedures of the definition of P to T in all object o1 generation with o2, the behavior of the program P did not change, then the type S is a subtype of type T, where all references to base classes must be able to transparently use objects of derived classes without knowing it. In plain English: a subclass can extend the functions of its parent class, but a subclass cannot modify the original functions of its parent class. The Richter’s substitution principle defines the use of inheritance
case
demand
There is now a calculator (superclass) that can do the addition, subtraction, multiplication, and division, defining its subclasses to demonstrate the problems inheritance can have
Plan a
Define the Calculator class calculator.java
/** * calculator class *@author:liyajie
* @createTime: 2022/1/31 arieh *@version: 1.0 * /
public class Calculator {
// Define the addition function
public int add(int a,int b){
return a + b;
}
// Define the subtraction function
public int sub(int a,int b){
returna - b; }}Copy the code
Define the SuperCalculator class supercalculator.java
/** * Supercalculator class *@author:liyajie
* @createTime: 2022/1/31 arieh *@version: 1.0 * /
public class SuperCalculator extends Calculator{
// Add the two numbers and add 5
@Override
public int add(int a,int b){
return a + b + 5;
}
// We want the sum of the two numbers to be equal to 100
public int mul(int a,int b){
int count = add(a, b);
return 100- count; }}Copy the code
Define the test class test1.java
/** * Test class 1 *@author:liyajie
* @createTime: 2022/1/31 arieh *@version: 1.0 * /
public class Test1 {
public static void main(String[] args) {
int result = new Calculator().add(4.6);
System.out.println(The sum of 4 and 6 is: + result);
int mul = new SuperCalculator().mul(4.6);
System.out.println("The sum of 4 and 6 differs from 100 :"+ mul); }}Copy the code
Test result: You can see that the difference between 4 and 6 and 100 is 85, which is clearly the wrong answer. The reason for the error was that the SuperCalculator class inherited the Calculator class and overwrote the Add method, which eventually produced the wrong answer when called
Scheme 2
Define the Base class base.java
/** * Base class *@author:liyajie
* @createTime: 2022/1/31 alone *@version: 1.0 * /
public class Base {}Copy the code
Modified supercalculatornew.java class
/** * Supercalculator class *@author:liyajie
* @createTime: 2022/1/31 arieh *@version: 1.0 * /
public class SuperCalculatorNew extends Base{
private Calculator calculator = new Calculator();
// Add the two numbers and add 5
public int add(int a,int b){
return a + b + 5;
}
// We want the sum of the two numbers to be equal to 100
public int mul(int a,int b){
int count = calculator.add(a, b);
return (100- count); }}Copy the code
The test class Test2. Java
/** * Test class 2 *@author:liyajie
* @createTime: 2022/1/31 arieh *@version: 1.0 * /
public class Test2 {
public static void main(String[] args) {
int result = new Calculator().add(4.6);
System.out.println(The sum of 4 and 6 is: + result);
int mul = new SuperCalculatorNew().mul(4.6);
System.out.println("The sum of 4 and 6 differs from 100 :"+ mul); }}Copy the code
Test results: You can see that the test results are correct
Comparison and analysis
Scheme 1 directly inherited the Calculator class and rewrote the non-abstract method add of the parent class, resulting in wrong results when calling Scheme 2 inherited the base class and injected the Calculator class as a dependency, followed the Principle of Richter substitution, and got the correct results
conclusion
- A subclass can implement an abstract method of the parent class, but cannot override a nonabstract method of the parent class
- Subclasses can extend their own methods
- The Richter substitution principle does not mean that we should try to avoid inheritance
- Richter’s substitution principle is one of the important ways to realize open – close principle
trailer
Today is New Year’s Eve, tomorrow is the first day of the New Year, xiaobian suspended update, I wish you all a happy New Year, Tiger Tiger alive and powerful, 2022, promotion and salary, to the peak of life! Seventh day, we fight again!