Public 및 Private 명령문

객체 지향 프로그래밍의 목표 중 하나는 최종 사용자의 구현 세부정보를 숨기고 블랙 박스를 제공하는 것입니다. 이러한 복잡성 캡슐화를 지원하기 위해 BSL을 사용하면 객체의 메소드 및 등록정보를 숨길 수 있습니다.

최종 사용자에게 숨겨진 메소드와 등록정보를 생성하려면, 등록정보 또는 메소드의 정의 앞에 Private 키워드를 추가합니다.

최종 사용자가 액세스할 수 있는 메소드 및 등록정보를 생성하려면 등록정보 또는 메소드 정의 앞에 Public 또는 Dim 키워드를 사용합니다. 메소드 또는 등록정보가 public, dim 또는 private인지 명시적으로 지정하지 않으면 public으로 설정됩니다.

그러나 등록정보 또는 메소드가 Public 또는 Private인지 항상 명시적으로 표시하는 것이 좋습니다.

클래스 인스턴스를 통해 private 메소드 또는 등록정보에 액세스하려고 하면 오류가 발생합니다.

예:

Class Comp
    Private modStrType
    Private Property Let ComputerType(strType)
        modStrType = strType
    End Property
    Public Property Get ComputerType
        ComputerType = modStrType
    End Property
    Private Function Funct1
        'Add business logic using class variables
    End Function
    Public Sub Sub1
        'Add business logic using class variables
    End Sub
End Class

Set objComp = new Comp
objComp.ComputerType = "Mac"        'Invalid, accessing this property will throw error as it’s private
str1 = objComp.ComputerType 
objComp.Funct1                           'Invalid, accessing this function will throw error as it’s private
objComp.Sub1