companion object - static Kotlin


class A {
companion object {
fun bar() {
println("Companion object called")
}
}

constructor() {
bar()
}
}

// A.bar()

class User private constructor(val nickname:String) {
companion object {
fun newSubscribingUser(email:String)=User(email.substringBefore('@'))
fun newFacebookUser(accountId:Int)=User(getFacebookName(accountId))
}
}

덧글

댓글 입력 영역