Joke Collection Website - Talk about mood - C# For example, talk about the Richter substitution principle.

C# For example, talk about the Richter substitution principle.

Liskov Substitution Principle LSP is one of the basic principles of object-oriented design. The Richter substitution principle says that where any base class can

appear, subclasses can certainly appear. LSP is the cornerstone of inheritance and reuse. Only when a subclass can replace the parent class and the function of the software unit is not affected, the parent class can be truly reused, and the subclass can add new behaviors on the basis of

the parent class. Richter substitution principle is a supplement to the "on-off" principle. The key step to realize the "on-off" principle is abstraction. The inheritance relationship between the base class and the subclass is the concrete reality of abstraction, so the Richter substitution principle is the specification of the concrete steps to realize abstraction.

here are some examples to illustrate what the interior substitution principle is.

Example 1 Square is not a rectangle.

In geometry, a square is definitely a rectangle, but it is a special rectangle with all four sides equal. By using the object-oriented inheritance relationship, we let the square inherit the rectangle. The code is as follows:

// C# Explain the Richter substitution principle

// Example name: Square is not a rectangle <

// rectangular class

classrectangle

{

int length;

int width;

public int getLength()

{

return length;

}

public int getWidth()

{

return width;

}

public virtual void setLength(int length)

{

this.length = length;

}

public virtual void setWidth(int width)

{

this.width = width;

}

}

// square class square: rectangle

{

public override void setlength (int length)

{

base.setlength (length);

base.setWidth(length);

}

public override void setWidth(int width)

{

base.setLength(width);

base.setWidth(width);

}

}

/*

* Since the length and width of a square must be equal, in the methods setLength and setWidth,

* assign the same values to the length and width. Class TestRectangle is a component of our software system.

* It has a resize method to use the base class Rectangle. The function of the resize method is to simulate the gradual increase of the rectangle width

*/

/Test class test rectangle

Class test rectangle {

//This method changes the rectangle width. Set the width to be larger than the height

static void resize (rectangle objrect)

{

console. writeline ("Start setting width");

while(objRect.getWidth() < =

objRect.getLength() )

{

objRect.setWidth( objRect.getWidth () + 1 );

}

Console.WriteLine ("End of setting width");

}

static void main ()

{

//Instantiate a rectangle

Rectangle r=new Rectangle ();

// let the length be 1

r.setLength(1);

// Set the width to 1

r.setWidth(1);

Console.WriteLine ("rectangle");

Console.WriteLine ("length before change:" +r.getLength ());

Console.WriteLine ("width before change:" +r.getWidth ());

// Call the method to change the width of the rectangle

resize(r);

// after the change

Console.WriteLine ("the length after the change is:" +r.getLength ());

Console.WriteLine ("width after change:" +r.getWidth ());

Rectangle s=new Square();

s.setLength(1);

s.setWidth(1);

Console.WriteLine ("square");

Console.WriteLine ("length before change:" +s.getLength ());

Console.WriteLine ("width before change:" +s.getWidth ());

// Call the method to change the width of the square

resize(s);

Console.WriteLine ("changed length:" +s.getLength ());

Console.WriteLine ("width after change:" +s.getWidth ());

}

}

When we run this code, we will find that if we pass an example of an ordinary rectangular

shape as a parameter into the resize method, we will see the effect that the rectangular width gradually increases. When the width is greater than the length, the code will stop, and the result of this behavior is in line with our expectations; If we use the

Richter scale substitution to assign an instance of the subclass to the parent class, and then pass the instance of the parent class (square) as a parameter into the resize method, we will see that the width and length of the square are increasing, and the code will continue to run

until the system produces an overflow error. Therefore, an ordinary rectangle is suitable for this code, but a square is not.

We come to the conclusion that in the resize method, the Rectangle type parameter cannot be replaced by the Square type parameter, and if it is replaced, the expected result will not be obtained. Because of this, the inheritance relationship between Square class and Rectangle class violates the Richter substitution principle, and the inheritance relationship between them is untenable, and the square is not a rectangle.

When running the program, the rectangle can change the width of the rectangle normally, but the square can't change the width. The principle of Richter substitution shows that we can't use inheritance indiscriminately. The length and width of a rectangle and the length and width of a square are obviously different, so it is wrong for a square to inherit a rectangle. The correct way to write it is that we can abstract the square and rectangle, and then let the square and rectangle inherit this abstract class, so that the two classes don't

influence each other and realize their respective functions.

reference:/jiaping424/article/details/6683478

Thank you.