Kotlin

Kotlin - 가시성 지시자

J_Bin 2022. 6. 14. 15:01

* private : 접근 범위가 선언된 요소에서 한정됨

 

package chap02.section1


/* Private */

private class PrivateClass {
    private var i = 1
    
    private fun privateFunc(){
        i += 1
    }
    
    fun access(){
        privateFunc()
    }
}
class OtherClass {
    //val opc = PrivateClass()        // 불가 : opc가 private되거나 PrivateClass가 public이 되어야함
    private val opc = PrivateClass()    // 가능
    
    fun test(){
        val pc = PrivateClass()
    }
}



fun main(){
    
    val pc = PrivateClass()
    pc.i                    // 접근불가
    pc.privateFunc()        // 접근불가
    

}

fun TopFuncation(){
    val tpc = PrivateClass()        // 객체 생성 가능
}

 

* protected : 최상위에 선언된 요소에는 지정할 수 없고 클래스나 인터페이스와 같은 요소의 멤버에만 지정할 수 있다.

package chap02.section1


/* Protected */

open class Base {       // 최상위 클래스에는 protected를 사용할 수 없음
    protected var i = 1

    protected fun protectedFunc(){
        i += 1  // 접근 허용
    }

    fun access() {
        protectedFunc()     // 접근 허용
    }

    protected class Nested      // 내부 클래스에는 지시자 허용
}

class Derived : Base() {

    fun test(base:Base): Int {
        protectedFunc()         // Base 클래스의 메서드 접근 가능
        return i                // Base 클래스의 프로퍼티 접근 가능
    }

}



fun main(){

    val base = Base()                   // 생성 가능
    base.i                              // 접근 불가
    base.protectedFunc()                // 접근 불가
    base.access()                       // 접근 가능능


}

 

#  internal : 프로젝트 단위의 모듈에서의 접근자, 모듈이 달라지면 접근 불가능

package chap02.section1


/* internal */

/* 파일이 달라져도 동일한 모듈에 있다면 바로 접근할 수 있다.*/

internal class InternalClass {
    internal var i : Int = 1

    internal fun icFunc() {
        i += 1
    }

    fun access() {
        icFunc()
    }

}

class Other {
    internal val ic = InternalClass()

    fun test() {
        ic.i
        ic.icFunc()
    }
}





fun main(){

    val mic = InternalClass()
    mic.i
    mic.icFunc()




}

 

 

# review

package chap02.section1

open class Car protected constructor(_year : Int, _model : String, _power : String, _wheel : String){

    private var year : Int = _year
    public var model : String = _model
    protected open var power : String = _power
    internal var wheel : String = _wheel

    protected fun start(key : Boolean) {
        if (key) println("Start the Engine!")
    }

    class Driver(_name : String, _license : String) {
        private var name : String = _name
        var license : String = _license
        internal fun driving() = println("[Driver] Driving() - $name")
    }
}

class Tico (_year: Int, _model: String, _power: String, _wheel: String, var name : String, private var key : Boolean) : Car(_year, _model, _power, _wheel) {

    override var power : String = "50hp"
    val driver = Driver(name, "first class")

    constructor(_name: String, _key : Boolean) : this(2014,"basic","100hp","normal",_name, _key){
        name = _name
        key = _key
    }

    fun access(password : String) {
        if(password == "gotico") {
            println("----[Tico] access() --------")
            //super.year  // 접근 불가 (private)
            println("super.model : ${super.model}")         // public
            println("super.power : ${super.power}")         // protected
            println("super.wheel : ${super.wheel}")         // internal
            super.start(key)                                // protected

            //driver.name                                       // private 접근 불가
            println("Driver().license = ${driver.license}")     // public
            driver.driving()                                    // internal
        }else {
            println("You're a burglar!")
        }
    }
}

class SM5(_year: Int, _model: String, _power: String, _wheel: String, var name : String, private var key : Boolean) : Car(_year, _model, _power, _wheel){

}

class Burglar() {
    fun steal(anycar : Any) {
        if(anycar is Tico) {
            println("----[Burglar] steal() --------")
            //println(anycar.power)                                     // protected 접근 불가
            //println(anycar.year)                                      // private 접근 불가
            println("anycar.name = ${anycar.name}")                     // public 접근
            println("anycar.wheel = ${anycar.wheel}")                   // internal 접근(같은 모듈이니깐 가능)
            println("anycar.model = ${anycar.model}")                   // public 접근

            println(anycar.driver.license)
            anycar.driver.driving()                                 // internal 접근(같은 모듈이니깐 가능)
            //println(Car.start())                                  // protected 접근 불가
            anycar.access("dontknow")
        } else {
            println("Nothing to steal")
        }
    }
}


fun main()  {

    //val car = Car()       // protected 생성 불가
    val tico = Tico("kilding",true)
    tico.access("gotico")

    val burglar = Burglar()
    burglar.steal(tico)



}