본문 바로가기

Language/Kotlin

[Kotlin] 가시성 변경자(Visibility Modifier)

[작성중]

 

코틀린의 가시성 변경자 

변경자 클래스 멤버 최상위 선언
public 모든 곳에서 볼 수 있다. 모든 곳에서 볼 수 있다.
internal 같은 모듈 안에서만 볼 수 있다. 같은 모듈 안에서만 볼 수 있다.
protected 하위 클래스 안에서만 볼 수 있다. (최상위 선언에 적용할 수 없다)
private 같은 클래스 안에서만 볼 수 있다. 같은 파일 안에서만 볼 수 있다.

 

가시성 관련 예제

// 확장 함수 컴파일 오류를 없애기 위해, 가시성을 "public"으로 변경
public open class TalkativeButton{
    fun yell() = println("Hey!")
    fun whisper() = println("Let's talk")
}


// 디폴트가 'public'임
fun TalkativeButton.giveSpeech(){
    yell()
    whisper()
}

fun main(){
    TalkativeButton().giveSpeech()
}

 

 

출처 : Kotlin in Action