kotlin (5) 썸네일형 리스트형 [Kotlin] Annotation & Annotation 처리에 관하여 Annotation을 사전에서 찾아보면 주석이다. 이 주석을 어디에 어떻게 추가하는지 알아보도록 하자. Kotlin Reflection interface의 hierarchy의 최상단은 annotatedElement 이다. 해당 인터페이스를 살펴보면 다음과 같다. 어노테이션을 기록하는 역할을 하는 것을 알 수 있다. Reflection interface의 최상단 interface가 위와 같다는 것은 reflection으로 다루던 모든 것이 annotation의 대상이 될 수 있다는 것을 알 수 있다. 아래는 코틀린 클래스의 annotation을 가져와 출력하는 예시이다. fun main() { val kClass = Playground::class kClass.annotations.forEach { ann.. [Kotlin] Reflection에 관하여 & Property에 관하여 Java에서 Class class를 통하여 Reflection을 수행했었다. Java와 완벽 호환되는 언어인 Kotlin은 이에 맞게 KClass를 지원한다. 예시로 KCallable과 KFunction을 살펴보도록 하자. /** * @param R return type of the callable. */ public actual interface KCallable : KAnnotatedElement { public val parameters: List public val returnType: KType @SinceKotlin("1.1") public val typeParameters: List public fun call(vararg args: Any?): R public fun callBy(args:.. [Kotlin] Companion Object에 대하여 Kotlin에서 companion object를 살펴보기 위해 간단한 예시 코드를 작성해보았다. class Example { companion object { var test = 1 } } fun main() { val example = Example.test } 위 코드를 자바 코드로 변환해 보면 다음과 같다. public final class Example { private static int test = 1; @NotNull public static final Companion Companion = new Companion((DefaultConstructorMarker)null); public static final class Companion { public final int getTest() {.. [Kotlin] 코틀린 Property[backing property, backing field] 자바 클래스에서 필드를 생성하고 getter와 setter를 사용하여 외부에서 값을 가져오거나 변경할때 사용하였다. 코틀린에서 말하는 property는 자바에서의 필드, getter, setter를 모두 묶어 있는 것을 말한다. 자바에서 필드를 생성하는 것과 같은 방법으로 프로퍼티를 만들게 되지만 코틀린에서 자동으로 기본 getter, setter를 생성해 준다. class A { var a: Int = 0 } 위 코틀린 클래스를 자바 클래스로 변경해 주면 다음과 같은 결과를 확인할 수 있다. public final class A { private int a; public final int getA() { return this.a; } public final void setA(int var1) { thi.. [Kotlin] Convert string number to primitive type 콘솔에서 입력한 숫자를 적당한 Primitive number로 변환해 보려고 한다. 리턴 범위는 Double, Float, Long, Int, Short, Byte로 제한하고 진행해 보자. 일단 정수와 실수는 쉽게 나눌 수 있을 것 같다. 입력받은 string에서 "." 이라는 힌트를 받을 수 있기 때문이다. 정수형의 경우는 다음과 같다. Long의 경우 8바이트-64비트-이므로 $[-2^{63}, 2^{63}-1]$ Int의 경우 4바이트-32비트-이므로 $[-2^{31}, 2^{31}-1]$ Short의 경우 2바이트-16비트-이므로 $[-2^{15}, 2^{15}-1]$ Byte의 경우 1바이트-8비트-이므로 $[-2^{7}, 2^{7}-1]$ 위와 같은 범위를 가지기 때문에 범위에 따라서 명확하게.. 이전 1 다음