Czy to rozwiązuje naruszenie prostokąta prostokąta Liskowa?
Jestem bardzo nowy w zasadach projektowania SOLID. Jedną z rzeczy, które miałem problem ze zrozumieniem, jest przykład „prostokąta prostokątnego” naruszenia zasady podstacji Liskowa. Dlaczego selektor wysokości / szerokości kwadratu powinien zastąpić te prostokąta? Czy to nie jest dokładnie to, co powoduje problem, gdy istnieje polimorfizm?
Czy usunięcie tego nie rozwiązuje problemu?
class Rectangle
{
public /*virtual*/ double Height { get; set; }
public /*virtual*/ double Width { get; set; }
public double Area() { return Height * Width; }
}
class Square : Rectangle
{
double _width;
double _height;
public /*override*/ double Height
{
get
{
return _height;
}
set
{
_height = _width = value;
}
}
public /*override*/ double Width
{
get
{
return _width;
}
set
{
_width = _height = value;
}
}
}
class Program
{
static void Main(string[] args)
{
Rectangle r = new Square();
r.Height = 5;
r.Width = 6;
Console.WriteLine(r.Area());
Console.ReadLine();
}
}
Wydajność wynosi 30 zgodnie z oczekiwaniami.