Public Available to eveyone
Private can only be referenced within this class
The protected inheritance class can be accessed.
Default can be accessed in the same package
Composition is to use the objects of other classes as their own members. When designing, you should first consider using composition instead of inheritance, because composition is more flexible and inheritance will complicate the design.
When a reference to a subclass object is assigned to a parent class to reference a variable, you can only access the part of the object defined by the parent class.
LSP (substitution principle) requires that the method access permission of the subclass is not less than the access permission of the method of the base class. During the design, we try to inherit from the abstract class rather than the specific class.
The Li's replacement principle is a supplement to the open and closed principle, which talks about the relationship between the base class and the subclass. Only when such a relationship exists can The Lishi replacement relationship exist.
"Square is a rectangle" is the most classic example of understanding the Li's replacement principle. In mathematics, a square is undoubtedly a rectangle. It is a rectangle with the same length and width. Therefore, the square should inherit from the rectangle.
The rectangle class is shown in program 1.
Program 1 rectangle. Java
Package principle. liskovsubstitution;
Public class rectangle {
Private int height;
Private int width;
Public int getheight (){
Return height;
}
Public void setheight (INT height ){
This. Height = height;
}
Public int getwidth (){
Return width;
}
Public void setwidth (INT width ){
This. width = width;
}
}
The square class that inherits the rectangle is shown in program 2.
Program 2 square class square. Java
Package principle. liskovsubstitution;
Public class square extends rectangle {
Public void setwidth (INT width ){
Super. setwidth (width );
Super. setheight (width );
}
Public void setheight (INT height ){
Super. setwidth (height );
Super. setheight (height );
}
}
Because the square length and width must be equal, the length and width values are assigned to the same value in the setlength () and setwidth () methods.
The function zoom () in the test class shown in program 3 is used to increase the length and width of the rectangle.
Program 3 test class testrectangle. Java
Package principle. liskovsubstitution;
Public class testrectangle {
Public void zoom (rectangle, int width, int height ){
Rectangle. setwidth (rectangle. getwidth () + width );
Rectangle. setheight (rectangle. getheight () + height );
}
}
Obviously, when the length and width are not the same, you cannot replace the rectangle with its subclass square. This violates the Lee's replacement principle.
To comply with the Rys replacement principle, we can create a parent base for Rectangles and squares, define common attributes, and define a zoom () abstract function,
As shown in program 4.
Program 4 parent class base. Java
Package principle. liskovsubstitution;
Public abstract class base {
Private int height;
Private int width;
Public int getheight (){
Return height;
}
Public void setheight (INT height ){
This. Height = height;
}
Public int getwidth (){
Return width;
}
Public void setwidth (INT width ){
This. width = width;
}
Public abstract void zoom (INT width, int height );
}
The rectangle class inherits from the parent class and writes its own zoom () implementation function, as shown in program 5.
Program 5: Modified baserectangle. Java
Package principle. liskovsubstitution;
Public class baserectangle extends base {
Public void zoom (INT width, int height ){
Setwidth (getwidth () + width );
Setheight (getheight () + height );
}
}
The square class also inherits from the parent class and writes its own zoom () implementation function, as shown in program 6.
Basesquare. Java
Package principle. liskovsubstitution;
Public class basesquare extends base {
Public void setwidth (INT width ){
Super. setwidth (width );
Super. setheight (width );
}
Public void setheight (INT height ){
Super. setwidth (height );
Super. setheight (height );
}
Public void zoom (INT width, int height ){
Int length = (width + height)/2;
Setwidth (getwidth () + length );
Setheight (getheight () + length );
}
}
Write test functions, as shown in program 7.
Test class basttest. Java modified after program 7
Package principle. liskovsubstitution;
Public class basttest {
Public void zoom (base, int width, int height ){
Base. Zoom (width, height );
}
}
In this case, the base class can be replaced by its subclass rectangle and square without changing the test code. This is the writing method that conforms to the Li's replacement principle.
Six principles of the design model:
The open/closed principle is open to extensions and closed to modifications.
Lee's replacement principle: the base class can be replaced by the quilt class.
The principle of merging and reuse: It depends on abstraction and not on specifics.
Interface isolation principle: it is better to use multiple isolated interfaces than to use a single interface.
Demeter's Law: A software entity should interact with other entities as little as possible.
Dependency inversion principle: Use synthesis/aggregation as much as possible, instead of inheritance.