S_pot
C#_캡슐화: 입력값 제한 시키는 방법 본문
// 캡슐화
class Box
{
private int width;
private int height;
public Box(int width, int height)
{
if (width > 0 && height > 0)
{
this.width = width;
this.height = height;
}
else
{
this.width = 0;
this.height = 0; // 음수입력을 막아줌
Console.WriteLine("입력값 오류로 변수 값이 모두 0으로 설정되었습니다.");
}
}
public int Area()
{
return this.width * this.height;
}
}
'C#' 카테고리의 다른 글
C#_get,set: 입력값 제한, 간단한 속성생성방법, propfull (0) | 2021.06.03 |
---|---|
C#_겟터(Getter), 셋터(Setter): 값을 안전하게 변경 (0) | 2021.06.03 |
C#_Generic(제네릭) 메서드: 코드의 중복을 최소화 (0) | 2021.06.03 |
C#_enum (0) | 2021.06.02 |
C#_continue키워드 (0) | 2021.06.01 |