Today, I was rejected by a girl. To continue writing this series.
I have previously written OCP (open and closed principle) and SRP (single responsibility principle ). Today's things are a little simpler.
Let's talk about LSP first. I can't remember his specific translation. His content is that the Child class must be able to replace the parent class. It seems to be the subclass replacement principle. I don't want to explain this too much. I want to understand all people who have learned Object-Oriented Knowledge. In inheritance, the parent class extracts the commonalities of the Child classes and extracts them. In this way, the Child class can replace the parent class. Here, we may only need to pay attention to the selection of inheritance. I forgot to see such a passage in any book. Are penguins birds? In real life, penguins are birds. However, for object orientation, penguins are not birds. Because the parent class of the bird should have a method of fly (), But penguins cannot implement this method.
Here, by the way, let's talk about the implementation principle of inheritance. We all know that in metadata, each class maintains a method table belonging to it. When declaring an object of a class, we first traverse all its parent classes on the entire object tree, then CLR creates a method table for each class, and when we instantiate a class, such as people P = new boy (); then three method tables are created, they are object, people, and boy, and then point the P pointer to the method table of people. In this case, we call the Eat () method. If we only hide (new) This method in the subclass, we call this method directly in Il. Otherwise, if this method is rewritten in the subclass, the pointer will be moved to the subclass to call the override method, which is reflected in Il, that is, callvirt.
Here, let's analyze the purpose of LSP. We know that the purpose of using the parent class is not just to write lessCodeAnd is more reusable for scalability, which is also one of the biggest advantages of object-oriented. We are certainly not unfamiliar with a concept, and are multi-state. The simplest example is as follows:
1 List < People > Peoplelist = New List < People > ();
2 Peoplelist. Add ( New Boy ());
3 Peoplelist. Add ( New GIRL ());
Assume that we inherit a bird class from people, then this code will be generated, peoplelist. Add (New Bird (), and the consequences can be imagined.
I want to give you more advice.