Daily Kotlin - 오버로딩, 오버라이딩, this, super, inner 클래스, public, protected, internal, public
- 오버로딩
package chap05.section3
fun main() {
val calc = Calc()
println(calc.add(2,3))
println(calc.add(3.14, 2.71))
println(calc.add(1, 2, 3))
println(calc.add("Hello ", "Kotlin"))
}
class Calc {
fun add(x: Int, y: Int): Int = x + y
fun add(x: Double, y: Double): Double = x + y
fun add(x: Int, y: Int, z: Int): Int = x + y + z
fun add(x: String, y: String): String = x + y
}
- 오버라이딩
override
키워드를 통해 오버라이딩
package chap05.section3.override
open class Bird(var name: String, var wing: Int, var beak: String, var color: String) {
fun fly() = println("Fly wing: $wing")
open fun sing(vol: Int) = println("Sing vol: $vol")
}
class Parrot(name: String, wing: Int = 2, beak: String, color: String, var language: String = "natural"): Bird(name, wing, beak, color) {
fun speak() = println("Speak! $language")
override fun sing(vol: Int) {
println("I'm a parrot! The volume level is $vol")
speak()
}
}
fun main() {
val parrot = Parrot(name = "myparrot", beak = "short", color = "multiple")
parrot.language = "English"
parrot.sing(5)
}
I'm a parrot! The volume level is 5
Speak! English
- this, super
package chap05.section4.personthis
open class Person {
constructor(firstName: String) {
println("[Person] firstname : $firstName")
}
constructor(firstName: String, age: Int) {
println("[Person] firstname : $firstName, $age")
}
}
class Developer: Person {
constructor(firstName: String): this(firstName, 10) {
println("[Developer] $firstName")
}
constructor(firstName: String, age: Int): super(firstName, age) {
println("[Developer] $firstName, $age")
}
}
fun main() {
val sean = Developer("Sean")
}
[Person] firstname : Sean, 10
[Developer] Sean, 10
[Developer] Sean
- inner 클래스에서 바깥 클래스 접근
package chap05.section4.innerref
open class Base {
open val x: Int = 1
open fun f() = println("Base Class f()")
}
class Child: Base() {
override val x: Int = super.x + 1
override fun f() = println("Child Class f()")
inner class Inside {
fun f() = println("Inside Class f()")
fun test() {
f()
Child().f()
super@Child.f()
println("[Inside] super@Child.x: ${super@Child.x}")
}
}
}
fun main() {
val c1 = Child()
c1.Inside().test()
}
Inside Class f()
Child Class f()
Base Class f()
[Inside] super@Child.x: 1
-
가시성 지시자 선언하지 않을 시 public이 기본
-
private
package chap05.section5
private class PrivateClass {
private var i = 1
private fun privateFunc() {
i += 1
}
fun access() {
privateFunc()
}
}
class otherClass {
//val opc = PrivateClass()
fun test() {
val pc = PrivateClass()
}
}
fun main() {
val pc = PrivateClass()
//pc.i
//pc.privateFunc()
}
fun TopFunction() {
val tpc = PrivateClass()
}
- protected
package chap05.section5
open class Base { //최상위(open) 클래스에는 protected 사용 불가
protected var i = 1
protected fun protectedFunc() {
i += 1 //접근 허용
}
fun access() {
protectedFunc() //접근 허용
}
protected class Nested //내부 클래스에는 protected 허용
}
class Derived: Base() {
fun test(base: Base): Int {
protectedFunc() //Base 클래스의 메소드 접근 가능
return i //Base 클래스의 property 접근 가능
}
}
fun main() {
val base = Base() // 생성 가능
//base.i // 접근 불가능
//base.protectedFunc() // 접근 불가능
base.access() // 접근 가능
}
- internal
package chap05.section5
internal class InternalClass {
internal var i = 1
internal fun icFunc() {
i += 1 //접근 허용
}
fun access() {
icFunc() //접근 허용
}
}
class Other {
internal val ic = InternalClass() //internal로 해야만 접근 가능
fun test() {
ic.i //접근 허용
ic.icFunc() //접근 허용
}
}
fun main() {
val mic = InternalClass() // 생성 가능
mic.i // 접근 허용
mic.icFunc() // 접근 허용
}
Comments