C#
C#_겟터(Getter), 셋터(Setter): 값을 안전하게 변경
S_pot
2021. 6. 3. 11:35
// 겟터와 셋터: 변수를 바로 변경할 수는 없지만 변수를 변경하는 메서드를 만든다.
// 변수값을 직접 입력하는 것이 아닌 메서드를 통해서 입력하게 해주는 것
public int Area()
{
return this.width * this.height;
}
// 겟터(Getter)
public int GetWidth() { return width; }
public int GetHeight() { return height; }
// 셋터(Setter)
public void SetWidth(int width)
{
if (width > 0) { this.width = width; }
else { Console.WriteLine("높이는 자연수를 입력해주세요"); }
}
public void SetHeight(int height)
{
if (height > 0) { this.height = height; }
else { Console.WriteLine("높이는 자연수를 입력해주세요"); }
}