[kotlin] 코틀린 클래스의 접근 제어 지시자
코틀린에서는 public
, internal
, protected
, private
등 네 가지의 접근 수준 지시자를 제공합니다. 이 중 public
이 기본값이며, 별도의 지시자를 사용하지 않으면 public
으로 적용됩니다.
다음은 코틀린 클래스와 멤버에 대한 접근 제어 지시자의 예시입니다.
public
class Car {
public val brand: String = "Toyota"
public fun start() {
// code to start the car
}
}
internal
internal class Truck {
internal val brand: String = "Ford"
internal fun start() {
// code to start the truck
}
}
protected
open class Vehicle {
protected val year: Int = 2021
protected fun honk() {
// code to honk the horn
}
}
private
class Motorcycle {
private val model: String = "Honda"
private fun start() {
// code to start the motorcycle
}
}
클래스나 멤버의 가시성을 지정할 때는 해당 지시자의 범위와 사용 가능한 위치에 주의해야 합니다. 더 자세한 내용은 코틀린 공식 문서를 참조하세요.