Liskov 원리를 설명하기 위해 노력하고 있는데, 아래 예제에서 sqaure에 너비를 설정하면 자동으로 동일한 길이로 설정되며 반대의 경우도 마찬가지입니다. 그러나 영역은 0으로 반환됩니다. 두 번째 경우에는 4x4 = 16 및 5x5 = 25가 필요합니다. 내가 도대체 뭘 잘못하고있는 겁니까? 기본 클래스의 속성을 재정의하는 방법에 있다고 생각합니다.유명한 Square/Rectangle 예제의 Liskov 원리가 논리적 인 오류를 낸다
using System;
public class Rectangle
{
public int length { get; set; }
public int breadth { get; set; }
public int area()
{
return length * breadth;
}
}
public class Square : Rectangle {
public new int length;
public new int breadth;
public new int Length
{
get
{
return this.length;
}
set
{
this.breadth = this.length = value;
}
}
public new int Breadth
{
get
{
return this.breadth;
}
set
{
this.breadth = this.length = value;
}
}
}
public class Program
{
public static void Main()
{
Square s = new Square();
s.length = 4;
s.breadth = 5;
int xx = s.area();
Console.Write(xx);
s.length = 5;
s.breadth = 4;
xx = s.area();
Console.Write(xx);
}
}
왜 기본 클래스 속성을 숨기고 있습니까? – Mahmoud
어떻게 그들을 숨길 수 있습니까? 모두 공개 되니? 설명 해주십시오? –
'new'는'override'가 아닙니다. 당신은 숨기고 오버라이드하지 않습니다. – Blorgbeard