ड्यूरिंग मैच ओलिंपियाड 250
Answers
Step-by-step explanation:
Liskov Substitution (The Substitution Principle)
// Perhaps, the principle that causes the greatest difficulties in understanding. The principle says - "Objects in the program can be replaced by their heirs without changing the properties of the program." In my words, I would say so - when using the class heir, the result of the code execution should be predictable and do not change the properties of the method. There is a classic example with a hierarchy of geometric shapes and area calculations. The example of code is below.
<?php
class Rectangle
{
protected $width;
protected $height;
public setWidth($width)
{
$this->width = $width;
}
public setHeight($height)
{
$this->height = $height;
}
public function getWidth()
{
return $this->width;
}
public function getHeight()
{
return $this->height;
}
}
class Square extends Rectangle
{
public setWidth($width)
{
parent::setWidth($width);
parent::setHeight($width);
}
public setHeight($height)
{
parent::setHeight($height);
parent::setWidth($height);
}
}
function calculateRectangleSquare(Rectangle $rectangle, $width, $height)
{
$rectangle->setWidth($width);
$rectangle->setHeight($height);
return $rectangle->getHeight * $rectangle->getWidth;
}
calculateRectangleSquare(new Rectangle(), 4, 5); // 20
calculateRectangleSquare(new Square(), 4, 5); // 25 ???
?>
// Obviously, such code is not executed as expected. But what's the problem? Is a "square" is not a "rectangle"? Yes, but in geometric terms. In terms of the same objects, the square is not a rectangle, because the behavior of the "square" object does not agree with the behavior of the "rectangle" object.
// Ok, so how to solve the problem? The solution is closely related to the notion of contract design. The description of designing under the contract can take not one article, therefore we will be limited to features which concern the Liskov principle. Contract design leads to some limitations on how contracts can interact with inheritance, namely:
// Preconditions can not be strengthened in a subclass.
// Postconditions can not be weakened in a subclass.
lol lol lol lol lol lollollol