나는 Cube 클래스를 가지고 있습니다. 큐브는 width=height=length
이되도록 구속 될 수 있습니다. 이 코드를 코딩했지만 알 수 있듯이 내 코드는 순환/무한 루프가됩니다 (the width sets the height which sets the width which sets the height and so on
).관련 속성 : 무한 루프를 피하는 방법
내 큐브 클래스를 제한하고이 무한 루프를 피하려면 어떻게해야합니까? 부울 변수 propagate
(아래 참조)을 사용하는 유일한 해결책은 무엇입니까?
public class Cube {
public bool isConstrained {get; set;} // if constrained then width=height=length
// Causes a circular problem. How can I avoid this? Maybe create a variable private bool propagate = false; ??
public double width
{
get { return width; }
set
{
width = value;
if (isConstrained)
{
height = value;
length = value;
}
}
}
public double height
{
get { return height; }
set
{
height = value;
if (isConstrained)
{
width = value;
length = value;
}
}
}
public double length
{
get { return length; }
set
{
length = value;
if (isConstrained)
{
height = value;
width = value;
}
}
}
}
내 유일한 해결책은 이것이다 :
private bool propagate = true;
public double length
{
get { return length; }
set
{
length = value;
if (isConstrained && propagate)
{
propagate = false;
height = value;
width = value;
propagate = true;
}
}
}
개인 필드가 아닌 이유는 무엇입니까? – Mephy
@Mephy 이것이 어떻게 내 문제를 해결할 수 있는지 이해하지 못합니까? 내 목표는 사람들이 외부 (공개)에서 큐브 속성을 설정할 수 있지만 큐브가 제한되어있을 때 너비와 높이를 변경하려는 너비를 설정했을 때 (너비 값까지). –