๊ด€๋ฆฌ ๋ฉ”๋‰ด

Welcome! Everything is fine.

[TIL] 220128 ๋ณธ๋ฌธ

TIL

[TIL] 220128

๊ฐœ๋ฐœ๊ณฐ๋ฐœ 2022. 1. 28.
728x90

๐Ÿ’ป Today I Leaned...

๐Ÿ“Œ ์ฝ”ํ‹€๋ฆฐ 3๊ฐ•์œผ๋กœ ๋๋‚ด๊ธฐ - 1ํŽธ ๊ธฐ๋ณธ๋ฌธ๋ฒ•

์ด์ „๊ณผ ์ด์–ด์ง€๋Š” ์ •๋ฆฌ๊ธ€๐Ÿ‘€

 

[TIL] 220127

๐Ÿ’ป Today I Leaned... ๐Ÿ“Œ ์ฝ”ํ‹€๋ฆฐ 3๊ฐ•์œผ๋กœ ๋๋‚ด๊ธฐ - 1ํŽธ ๊ธฐ๋ณธ๋ฌธ๋ฒ• 1. ํ•จ์ˆ˜ ์ œ์ผ ๋จผ์ €, ์ฝ”ํ‹€๋ฆฐ์˜ ์—”ํŠธ๋ฆฌ ํ•จ์ˆ˜๋Š” fun main()ํ•จ์ˆ˜์ด๋‹ค. fun main(){ } ์ž๋ฐ”์˜ ๊ฒฝ์šฐ, ๋งจ ์•ž์— ๋ฆฌํ„ดํƒ€์ž…์„ ์ ์–ด์คฌ์ง€๋งŒ ์ฝ”ํ‹€๋ฆฐ์€ ์–ด๋–ค

3uomlkh.tistory.com

5. ๋ฐฐ์—ด(Array)๊ณผ ๋ฆฌ์ŠคํŠธ(list)

1) Array

array๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Mutable์ด๋ฉฐ, ์ •ํ•ด์ ธ ์žˆ๋Š” ์‚ฌ์ด์ฆˆ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์ด์ฆˆ๋งŒ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์œผ๋ฉด ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋Š” array์˜ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ 3์œผ๋กœ ๋ณ€๊ฒฝํ•œ ๊ฒƒ์ด๋‹ค. array๋Š” arrayOf๋กœ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.

fun array(){

    val array = arrayOf(1, 2, 3)
    val array2 = arrayOf(1, "a", 3.4f)

   array[0] = 3
   
}

2) list

list๋Š” ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•œ (immutable) list์™€ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•œ  Mutable list ๋กœ ๋‚˜๋‰œ๋‹ค. list๋Š” listOf๋กœ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค. list๋Š” array์™€ ๋‹ค๋ฅด๊ฒŒ ์ธํ„ฐํŽ˜์ด์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— ์›์†Œ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. get์„ ์ด์šฉํ•˜์—ฌ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ ๋’ค ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ๋„ฃ์„ ์ˆœ ์žˆ์ง€๋งŒ ์ง์ ‘์ ์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค.

fun array(){
	// list
    val list = listOf(1, 2, 3)
    val list2 = listOf(1, "a", 3.4f)

    list[0] = 3 // ์—๋Ÿฌ
    var result = list.get(0)
}

Mutable list๋Š” ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๊ณ , ๊ฐ’์„ ๋ณ€๊ฒฝ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋Š” Mutable list์˜ ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ arrayList๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ ๋ชจ์Šต์ด๋‹ค. var์„ val๋กœ ๋ฐ”๊ฟ”๋„ ๋˜๋Š”๋ฐ, ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๋ฉด val๋กœ ํ•˜๋ฉด ์•ˆ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ arrayList์˜ ์ฃผ์†Œ๊ฐ’์€ ๋ฐ”๋€Œ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— val์„ ์‚ฌ์šฉํ•ด๋„ ์ƒ๊ด€์—†๋‹ค. ๋‹จ, ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•ด์„œ ์ฃผ์†Œ๊ฐ’์ด ๋ฐ”๋€” ๊ฒฝ์šฐ์—๋Š” val์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

fun array(){

    // Mutable list
    var arrayList = arrayListOf<Int>()
    arrayList.add(10)
    arrayList.add(20)

}

6. For / While

1) For

in ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ˜๋ณต ์กฐ๊ฑด๋ฌธ์„ ์“ด๋‹ค. ์ฒซ ๋ฒˆ์งธ for๋ฌธ์„ ๋ณด๋ฉด students ์•ˆ์— ์žˆ๋Š” ์›์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ๋ฐ˜๋ณตํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ for๋ฌธ์€ 1๋ถ€ํ„ฐ 10๊นŒ์ง€ 2๊ฐœ์”ฉ ๊ฑด๋„ˆ๋›ฐ๋ฉฐ(step) ๋”ํ•œ ํ›„ ๊ทธ ํ•ฉ๊ณ„๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. 

fun forAndWhile(){

    val students = arrayListOf("gom", "cong", "rang", "bam")

    for (name in students){
        println("${name}")
    }

    var sum : Int = 0
    for (i in 1..100 step 2){
        sum += i
    }
    println(sum)
}

ํ˜น์€ until์„ ์ด์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, 1..100์„ ์ผ์„ ๋•Œ์™€ ๋‹ค๋ฅธ ์ ์€ 100์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

    var sum : Int = 0
    for (i in 1 until 100){ //1..100๊ณผ ๋‹ค๋ฅธ์ ? 100์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค.
        sum += i
    }
    println(sum)

downTo๋ฅผ ์ด์šฉํ•˜์—ฌ 10๋ถ€ํ„ฐ 1๊นŒ์ง€ ์—ญ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

    for (i in 10 downTo 1){
        println(i)
    }

index์™€ name์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ, withIndex()๋ฅผ ๋’ค์— ๋ถ™์—ฌ์ฃผ๋ฉด ๋œ๋‹ค.

    var index = 0
    for ((index, name) in students.withIndex()){
        println("${index + 1}๋ฒˆ์งธ ํ•™์ƒ : ${name}")
        index++
    }

2) While

while๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์“ด๋‹ค.

    var index = 0
    while(index < 10){
        println("current index : ${index}")
        index++
    }

7. Nullable / NonNull

์ฝ”ํ‹€๋ฆฐ์—๋Š” Nullable์™€  NonNull ํƒ€์ž…์ด ์žˆ๋Š”๋ฐ, ์ž๋ฐ”์™€ ๋‹ค๋ฅธ ์  ์ค‘ ํ•˜๋‚˜๋‹ค. ์ž๋ฐ”์—์„œ NPE(Null Pointer Exception)๋Š” ์ปดํŒŒ์ผ ์‹œ์ ์—์„œ ์žก์„ ์ˆ˜ ์—†๊ณ , ๋Ÿฐํƒ€์ž„์—์„œ๋งŒ ์žกํžˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋”ฉ์— ๋ถˆํŽธํ•จ์ด ์žˆ์—ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ์ปดํŒŒ์ผ ์‹œ์ ์— ์žก์•„์ฃผ๋Š”๋ฐ ?๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

์•„๋ž˜ ์ฝ”๋“œ์—์„œ name์€ ๊ธฐ๋ณธ์ ์œผ๋กœ NonNull ํƒ€์ž…์œผ๋กœ, ํƒ€์ž…์„ ์ƒ๋žตํ•ด๋„ ๋œ๋‹ค.

nullName์€ ? ์—†์ด ๊ทธ๋ƒฅ ์“ฐ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€๋งŒ ?๋ฅผ ๋ถ™์ด๋ฉด Nullable ํƒ€์ž…์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— null์„ ๋„ฃ๊ณ  ์‹ถ๋‹ค๋ฉด ํƒ€์ž… ๋’ค์— ? ํ•˜๋‚˜๋งŒ ์จ์ฃผ๋ฉด ๋œ๋‹ค.

fun nullcheck(){

    var name : String = "gom" // NonNUll type
    var nullName : String? = null // ? ํ•˜๋‚˜๊ฐ€ ๋ถ™์—ฌ์ง€๋ฉด Nullable type

}

์–ด๋–ค function์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ, ๋ณ€์ˆ˜๊ฐ€ null์ด๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒดํฌ๋ฅผ ํ•ด์•ผํ•œ๋‹ค. uppercase()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ, nullNameInUpperCase ์—์„œ ?๋ฅผ ๋ถ™์ด๋ฉด nullName์ด Null์ผ ๋•Œ Null์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์•„๋‹ˆ๋ฉด uppercase()๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

fun nullcheck(){

    var name : String = "gom"
    var nullName : String? = null 

    var nameInUpperCase : String = name.uppercase()
    var nullNameInUpperCase : String? = nullName?.uppercase()
    //Null์ด๋ฉด Null์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์•„๋‹ˆ๋ฉด uppercase() ์‹คํ–‰

}

?: ๋Š” ์—˜๋น„์Šค ์—ฐ์‚ฐ์ž์ด๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋งŒ์•ฝ lastName์ด ์žˆ์œผ๋ฉด(Null์ด ์•„๋‹ˆ๋ฉด) ์ถœ๋ ฅํ•˜๊ณ , Null์ด๋ฉด "No lastName"์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๋””ํดํŠธ ๊ฐ’์„ ์ค€ ๊ฒƒ์ด๋‹ค.

fun nullcheck(){

    var name : String = "gom"
    
    // ?: ์—˜๋น„์Šค ์—ฐ์‚ฐ์ž
    val lastName : String? = null
    val fullName = name + " " + (lastName?: "No lastName")
    println(fullName)

}

? ์™€ ?: ๋งŒํผ์ด๋‚˜ ๋งŽ์ด ์“ฐ์ด๋Š” !!๋Š” ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ "์ด๊ฑฐ Null ์•„๋‹ˆ์•ผ!!"๋ผ๊ณ  ๋งํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ null์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด Null type Exception์„ ๋งŒ๋“ค์–ด๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ํ™•์‹คํ•˜์ง€ ์•Š์€ ์ด์ƒ ์ง€์–‘ํ•œ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ str์ด null์ด ์•„๋‹˜์„ ํ™•์‹ ์‹œ์ผฐ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ์ค„์—์„œ uppercase()๋ฅผ ์“ธ ๋•Œ๋„ ?๋ฅผ ๋ถ™์ด์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

fun ignoreNulls(str : String?){

    val mNotNull : String = str!!
    val upper :String = mNotNull.uppercase()

}

๋งˆ์ง€๋ง‰์œผ๋กœ let ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด Not null์ธ ๊ฒฝ์šฐ, ํŠน์ • ๊ตฌ๋ฌธ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. let์€ ์ž์‹ ์˜ receiver ๊ฐ์ฒด๋ฅผ ๋žŒ๋‹ค์‹ ๋‚ด๋ถ€๋กœ ์˜ฎ๊ฒจ์„œ ์‹คํ–‰ํ•˜๋Š” ๊ตฌ๋ฌธ์ด๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ receiver ๊ฐ์ฒด์ธ email์ด null์ด ์•„๋‹ˆ๋ผ๋ฉด ๋žŒ๋‹ค์‹ ๋‚ด๋ถ€๋กœ ์˜ฎ๊ฒจ ์‹คํ–‰ํ•œ๋‹ค๋Š” ์†Œ๋ฆฌ๋‹ค. 

fun ignoreNulls(str : String?){

    val email : String? = "gom12@naver.com"
    
   // email์ด Null์ด ์•„๋‹ˆ๋ฉด {}์•ˆ์— ์žˆ๋Š” ๊ฑธ ํ•จ.
    email?.let{ 
        println("My email is ${email}")
    }
}

8. Class

โ” ์ฝ”ํ‹€๋ฆฐ์ด ์ž๋ฐ”์™€ ๋‹ค๋ฅธ ์ 

  • ๊ผญ ํŒŒ์ผ ์ด๋ฆ„๊ณผ ํด๋ž˜์Šค ์ด๋ฆ„์ด ์ผ์น˜ํ•˜์ง€ ์•Š์•„๋„ ๋จ.
  • ์—ฌ๋Ÿฌ ํด๋ž˜์Šค๋„ ํ•œ ํŒŒ์ผ ์•ˆ์— ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ.

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ํด๋ž˜์Šค๋ฅผ ์“ฐ๋Š” ๊ธฐ๋ณธ์ ์ธ ๋‚ด์šฉ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ž๋ฐ”์—์„œ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ new ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“ค์—ˆ์ง€๋งŒ, ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ๊ทธ๋ƒฅ ์“ฐ๋ฉด ๋œ๋‹ค.

class Human{

    val name = "gom"

    fun eatingCake(){
        println("This is so YUMMMYYY!!!")
    }
}

fun main(){
    val human = Human()
    human.eatingCake()

    println("this human's name is ${human.name}")
}

๋งŒ์•ฝ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ด๋ฆ„์„ ์ •์˜ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜๋ฉด ๋˜๋Š”๋ฐ, ์ƒ์„ฑ์ž ๋ธ”๋ก์„ ๋”ฐ๋กœ ๋งŒ๋“œ๋Š” ์ž๋ฐ”์™€ ๋‹ฌ๋ฆฌ ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ํด๋ž˜์Šค ์ด๋ฆ„  ์˜†์— constructor๋ฅผ ์ด์šฉํ•ด ์ ์–ด์ค€๋‹ค. constructor๋Š” ์ƒ๋žต๊ฐ€๋Šฅํ•˜๋ฉฐ, ์•„๋ž˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ๋””ํดํŠธ ๊ฐ’(์˜ˆ์‹œ์—์„  "Anonymous")๋„ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„ฃ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

class Human constructor(val name : String = "Anonymous"){

    fun eatingCake(){
        println("This is so YUMMMYYY!!!")
    }
}

fun main(){
    val human = Human("gom")
    human.eatingCake()

    println("this human's name is ${human.name}")
}

๋˜ํ•œ ์ƒ์„ฑ์ž์—์„œ ์ฝ”๋“œ๋ธ”๋ก์€ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด init์„ ์‚ฌ์šฉํ•œ๋‹ค. init ์—ญ์‹œ ์ฃผ์ƒ์„ฑ์ž์˜ ์ผ๋ถ€๋ผ์„œ ์ œ์ผ ๋จผ์ € ์‹คํ–‰๋œ๋‹ค. ์ฃผ ์ƒ์„ฑ์ž๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ถ€์ƒ์„ฑ์ž๋„ ์—ฌ๋Ÿฌ๊ฐœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. (์ž๋ฐ”์˜ ๊ฒฝ์šฐ ์˜ค๋ฒ„๋กœ๋”ฉ์— ํ•ด๋‹น) ๋ถ€์ƒ์„ฑ์ž๋Š” ํ•ญ์ƒ this๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ฃผ์ƒ์„ฑ์ž์˜ ์œ„์ž„์„ ๋ฐ›์•„์•ผ ํ•œ๋‹ค.

class Human constructor(val name : String = "Anonymous"){

    // ๋ถ€์ƒ์„ฑ์ž
    constructor(name : String, age : Int) : this(name){ // ์ฃผ์ƒ์„ฑ์ž๊ฐ€ ์—†์œผ๋ฉด this(name)๊ฐ€ ํ•„์š”X
        println("My name is ${name}, ${age}year's old.")
    }

    // ์ƒ์„ฑ์ž์—์„œ ์ฝ”๋“œ๋ธ”๋ก์„ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด init
    init {
        println("New Human has been born!!")
    }
    
    fun eatingCake(){
        println("This is so YUMMMYYY!!!")
    }
    
}

fun main(){
    val human = Human("gom")
    val human2 = Human("gom", 2)
    human.eatingCake()

    println("this human's name is ${human.name}")
}

์ƒ์†์„ ํ•ด์•ผํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ? ์ฝ”ํ‹€๋ฆฐ์˜ ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ final ํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— class ์•ž์— open ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ฃผ์–ด์•ผ ์ƒ์†์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ์ƒ์†์€ ์ž๋ฐ”์™€ ๊ฐ™์ด ํ•œ ๊ฐœ๋ฐ–์— ๋ชปํ•˜๊ณ , ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•  ๋•Œ๋„ fun ์•ž์— open ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

open class Human constructor(val name : String = "Anonymous"){ 
    
    init {
        println("New Human has been born!!")
    }

    open fun singASong(){ // ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•  ๋•Œ๋„ open ์ ์–ด์ค˜์•ผ ํ•จ.
        println("lalala")
    }
}

class Korean : Human(){ 
    override fun singASong(){ 
        println("๋ผ๋ผ๋ผ")
    }

}

fun main(){

    korean.singASong()
}

๋งŒ์•ฝ Human ํด๋ž˜์Šค์™€  Korean ํด๋ž˜์Šค์˜ singAsong()์„ ๋‘˜ ๋‹ค ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด super.singAsong() ์ฒ˜๋Ÿผ super๋ฅผ ์ด์šฉํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

open class Human constructor(val name : String = "Anonymous"){ 
    
    init {
        println("New Human has been born!!")
    }

    open fun singASong(){
        println("lalala")
    }
}

class Korean : Human(){ 
    override fun singASong(){ 
        super.singASong() 
        println("๋ผ๋ผ๋ผ")
        println("My name is : ${name}")
    }

}

fun main(){

    val korean = Korean()
    korean.singASong()
}

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[TIL] 220203  (0) 2022.02.03
[TIL] 220130  (0) 2022.01.30
[TIL] 220127  (0) 2022.01.27
[TIL] 220125  (0) 2022.01.25
[TIL] 220122  (0) 2022.01.22