강의

멘토링

커뮤니티

Inflearn Community Q&A

No author

This post's author information has been deleted.

Kim Young-han's Practical Java - Intermediate Part 2

Problem and Solution 2

shuttle.showinfo를 호출시 실행창 질문

Written on

·

275

·

Edited

0

package generic.test.ex3

object UnitPrinter { //제네릭 메서드


    fun <T : BioUnit>printerV1(t: Shuttle<T>){
        println(t.showInfo())
    }
    fun printerV2(shuttle: Shuttle<out BioUnit>){
        println(shuttle.showInfo())
    }
    fun <T : BioUnit>printerV3(t: Shuttle<T>){
        val unit = t.out()
        println("이름: ${unit.name}  hp:${unit.hp}")
    }
    fun printerV4(shuttle: Shuttle<out BioUnit>){
        val unit = shuttle.out()
        println("이름: ${unit.name}  hp:${unit.hp}")
    }
}

자바와 코틀린은 100퍼센트 호환이 된다고 해서 코틀린으로 강의를 보고있습니다.

강의에선 v1과 v2로 인자로 받은 셔틀에서 unit을 꺼내어 내용을 출력했는데요. 그렇게 안하고 셔틀에서 바로showinfo를 호출해서 실행창에

name : 마린 hp : 40

kotlin.Unit

name : 마린 hp : 40

kotlin.Unit

이렇게 줄바꿈으로 kotlin.Unit이라는게 자동적으로 붙는데 왜이런건가요 . 강의에서처럼 unit을꺼내어서 출력하면 안붙습니다.

java객체지향코딩-테스트알고리즘

Answer 2

0

shuttle.showInfo() 가 이미 출력하는 함수인데 그걸 또 println 하니까 코틀린이 알아서 뭐 해줬다 이런거 아닐까요 ?

0

yh님의 프로필 이미지
yh
Instructor

안녕하세요. 준호님

코틀린 관련해서는 도움을 드리기 어렵습니다.

감사합니다.

No author

This post's author information has been deleted.

Ask a question