클래스 내에서 클래스 변수에 대한 속성을 부여해줌
기존의 Get, Set 클래스 매서드를 사용하여 캡슐화 한 것을
클래스 변수에 대해 보다 직관적이게 사용할 수 있음
class Box
{
private int width;
public int Width
{
get { return width; } // get set 클래스 매서드 대신에 property
set
{
if (value > 0) { this.width = value; }
else { Console.WriteLine("너비는 0보다 큰 자연수 입력."); }
}
}
private int height;
public int Height
{
get { return height; }
set
{
if (value > 0) { this.height = value; }
else { Console.WriteLine("높이는 0보다 큰 자연수 입력."); }
}
}
/* 생성자 */
public Box(int width, int height)
{
if (width > 0 && height > 0)
{
this.width = width;
this.height = height;
}
else
{
Console.WriteLine("박스의 너비와 높이는 0보다 큰 자연수 입력.");
}
}
public int Area() { return this.width * this.height; }
}