[코틀린 인 액션] CH9 제네릭스 Quiz

0

코틀린 인 액션

목록 보기
12/13
post-thumbnail

[코틀린 인 액션] CH9 제네릭스 Quiz

이 포스팅은 <Kotlin in Action>, 드미트리 제메로프 & 스베트라나 이사코바, 에이콘출판사(2017)을 읽고 개인 학습용으로 정리한 글입니다.

open class Dog{} 
class Maltese: Dog(){}

fun main(){
    val dog1 = Dog()
    val dog2 = Maltese()
    
    whatKindOfDog(dog1) //출력: It is just a dog
    whatKindOfDog(dog2) //출력: It is a maltese
}
  • 다음 조건을 만족하도록 whatKindOfDog 메서드를 작성하라
    • Dog 클래스를 상한으로 하는 타입 파라미터를 갖는다
    • Dog 클래스의 객체인지 Maltese 클래스의 객체인지 검사하여 결과를 출력한다

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

inline fun <reified T : Dog> whatKindOfDog(dog : T){
    if(dog is Maltese) println("It is a maltese")
    else println("It is just a dog")
}
profile
Be able to be vulnerable, in search of truth

0개의 댓글