Lisian Substitution Principle (LSP)

Table of contents

Introduction:

effect:

process:

Summarize:


Introduction:

The proposer of the Liskov Substitution Principle (LSP for short) is American computer scientist Barbara Liskov. Barbara Liskov is a computer scientist, a professor at MIT, and the first female PhD in computer science in the United States. She studied under Turing Award winner Professor John McCarthy, the proposer of the concept of artificial intelligence. She first proposed this principle in a 1986 paper and further elaborated and promoted it in subsequent research.

effect:

1. Maintainability:
Subclasses can completely replace parent classes, making code maintenance easier. Because if the subclass cannot replace the parent class, then when the parent class changes, the subclass will also need to be modified accordingly, which will increase the difficulty of code maintenance.
2. Extensibility:
Subclasses can inherit the properties and methods of the parent class, and can add their own properties and methods, thus extending the functions of the parent class. In this way, when new functions need to be added, you only need to add the corresponding attributes and methods in the subclass without modifying the parent class, which improves the scalability of the code. 3. Correctness Subclasses can be
completely
replaced Parent class, thus ensuring the correctness of the code. If the subclass cannot completely replace the parent class, then some errors will occur when using the subclass object because the behavior of the parent class may change, causing the code to behave inconsistently.
4. Reusability:
Subclasses can completely replace parent classes, thereby improving code reusability. If a subclass can replace the parent class, the same set of code can be used in different contexts, thus improving code reusability.

process:

Summarize:

In the above example, an Animal class and a Dog class are defined. The Dog class inherits from the Animal class. There is an Eat method in the Animal class, and there is also an Eat method in the Dog class. This method uses the new keyword to hide the Eat method in the Animal class.
In the Main method, an Animal object is created first, and then a Dog object is created. Then the Eat method of the animal object is called, and "I am eating." is output. This method calls the Eat method in the Animal class. Then, we call the Eat method of the dog object and output "I am a dog and I am eating." This method calls the Eat method in the Dog class.
Finally, the dog object is assigned to the animal object, and the Eat method of the animal object is called again, outputting "I am a dog and I am eating." This method calls the Eat method in the Dog class, because at this time The type of animal object is Dog type.
This example demonstrates the application of the Liskov substitution principle, that is, when the program is running, if a subclass object needs to be used to replace a parent class object, the program should be able to handle this substitution correctly without additional processing.

Guess you like

Origin blog.csdn.net/weixin_59272777/article/details/132468906